python - 如何将 pandas DataFrame 中的行从 Series 转换回 DataFrame?

标签 python pandas

我正在迭代 pandas DataFrame 的行,将每一行扩展为 N 行,并在每一行上添加附加信息(为简单起见,我将其设为这里是随机数):

from pandas import DataFrame
import pandas as pd
from numpy import random, arange

N=3
x = DataFrame.from_dict({'farm' : ['A','B','A','B'], 
                         'fruit':['apple','apple','pear','pear']})
out = DataFrame()
for i,row in x.iterrows():
    rows = pd.concat([row]*N).reset_index(drop=True)  # requires row to be a DataFrame
    out = out.append(rows.join(DataFrame({'iter': arange(N), 'value': random.uniform(size=N)})))

在此循环中,row 是一个 Series 对象,因此对 pd.concat 的调用不起作用。如何将其转换为 DataFrame? (例如x.ix[0:0]x.ix[0]之间的区别)

谢谢!

最佳答案

鉴于您的评论,我会尝试

def giveMeSomeRows(group):
    return random.uniform(low=group.low, high=group.high, size=N)

results = x.groupby(['farm', 'fruit']).apply(giveMeSomeRows)

这应该为您提供一个单独的结果数据框。我假设每种农场水果组合都是​​独一无二的...如果我们想更多地了解您的数据,可能还有其他方法。

更新

运行代码示例

def giveMeSomeRows(group):
    return random.uniform(low=group.low, high=group.high, size=N)

N = 3
df = pd.DataFrame(arange(0,8).reshape(4,2), columns=['low', 'high'])
df['farm'] = 'a'
df['fruit'] = arange(0,4)
results = df.groupby(['farm', 'fruit']).apply(giveMeSomeRows)

df

   low  high farm  fruit
0    0     1    a      0
1    2     3    a      1
2    4     5    a      2
3    6     7    a      3

结果

farm  fruit
a     0        [0.176124290969, 0.459726835079, 0.999564934689]
      1           [2.42920143009, 2.37484506501, 2.41474002256]
      2           [4.78918572452, 4.25916442343, 4.77440617104]
      3           [6.53831891152, 6.23242754976, 6.75141668088]

如果您想要一个数据框,您可以将函数更新为

def giveMeSomeRows(group):
    return pandas.DataFrame(random.uniform(low=group.low, high=group.high, size=N))

结果

                     0
farm fruit            
a    0     0  0.281088
           1  0.020348
           2  0.986269
     1     0  2.642676
           1  2.194996
           2  2.650600
     2     0  4.545718
           1  4.486054
           2  4.027336
     3     0  6.550892
           1  6.363941
           2  6.702316

关于python - 如何将 pandas DataFrame 中的行从 Series 转换回 DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23377546/

相关文章:

Python scikit-learn 预测失败

python - 如何及时转换这些列

python - 如何在 Pandas 中将数组列转换为 int 数组?

python - 使用正则表达式进行模式匹配 python

python - 如何从 pandas DF 获取 `field_name:field_type` 的字典

python - 根据一列的值从二维矩阵中提取值

python - 相当于 Python 文件的移动百分比

python - 如何在 pydot 中的两个子图之间添加边?

python - 从 PE 获取 IAT 和 EAT

python - 如何逐行应用函数,并在每一步保存 csv(追加)?