2
喜列的列表,所以我有以下数据框:累积使用GROUPBY
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
我的目标是GROUPBY水果和顺序,添加的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
但这不是累积性的,不能制作成一列。 感谢您的帮助