python - 使用 groupby 的列的累积列表

标签 python list pandas dataframe

您好,我有以下数据框:

   Fruit  metric
0  Apple     NaN
1  Apple   100.0
2  Apple     NaN
3  Peach    70.0
4   Pear   120.0
5   Pear   100.0
6   Pear     NaN

我的目标是按水果分组并按顺序将 metric 的每个非空值添加到具有自己单独列的累积列表中,如下所示:

   Fruit  metric  metric_cum
0  Apple     NaN          []
1  Apple   100.0       [100]
2  Apple     NaN       [100]
3  Peach    70.0        [70]
4   Pear   120.0       [120]
5   Pear   100.0  [120, 100]
6   Pear     NaN  [120, 100]

我试过这样做:

df['metric1'] = df['metric'].astype(str)
df.groupby('Fruit')['metric1'].cumsum()

但这会导致 DataError: No numeric types to aggregate

我也试过这样做:

df.groupby('Fruit')['metric'].apply(list)

导致:

Fruit
Apple      [nan, 100.0, nan]
Peach                 [70.0]
Pear     [120.0, 100.0, nan]
Name: metric, dtype: object

但这不是累加的,不能做成专栏。 感谢您的帮助

最佳答案

使用:

df['metric'] = df['metric'].apply(lambda x: [] if pd.isnull(x) else [int(x)])
df['metric_cum'] = df.groupby('Fruit')['metric'].apply(lambda x: x.cumsum())
print (df)
   Fruit metric  metric_cum
0  Apple     []          []
1  Apple  [100]       [100]
2  Apple     []       [100]
3  Peach   [70]        [70]
4   Pear  [120]       [120]
5   Pear  [100]  [120, 100]
6   Pear     []  [120, 100]

或者:

a = df['metric'].apply(lambda x: [] if pd.isnull(x) else [int(x)])
df['metric_cum'] = a.groupby(df['Fruit']).apply(lambda x: x.cumsum())
print (df)
   Fruit  metric  metric_cum
0  Apple     NaN          []
1  Apple   100.0       [100]
2  Apple     NaN       [100]
3  Peach    70.0        [70]
4   Pear   120.0       [120]
5   Pear   100.0  [120, 100]
6   Pear     NaN  [120, 100]

关于python - 使用 groupby 的列的累积列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719855/

相关文章:

python - Pandas GroupBy 全部应用

python - 如何循环分组的 Pandas 数据框?

Python,网络驱动程序错误(Selenium)

html - 描述列表使术语动态宽度,描述静态宽度

python - 将unicode列表转换为列表字符串

python - 通过列表理解重复列表项可变次数

python - 在列中,计算逗号分隔句子中的单词

python - 如何从处理中排除 Django 模板文件的一部分?

python - 如何使用 pandas 创建索引循环 FIFO 缓冲区

python - 在 Python 中检查绝对路径