gpt4 book ai didi

python - Pandas groupby : *full* join result of groupwise operation on original index

转载 作者:行者123 更新时间:2023-11-28 20:13:56 25 4
gpt4 key购买 nike

考虑这个df:

import pandas as pd, numpy as np

df = pd.DataFrame.from_dict({'id': ['A', 'B', 'A', 'C', 'D', 'B', 'C'],
'val': [1,2,-3,1,5,6,-2],
'stuff':['12','23232','13','1234','3235','3236','732323']})

问题:如何生成一个表,其中的列数与 unique id ({A, B, C}) 和与 df 一样多的行,例如对于对应于 id==A 的列,值为:

1,
np.nan,
-2,
np.nan,
np.nan,
np.nan,
np.nan

(这是 df.groupby('id')['val'].cumsum() 加入 df 索引的结果)。

最佳答案

UMMM 枢轴

pd.pivot(df.index,df.id,df.val).cumsum()
Out[33]:
id A B C D
0 1.0 NaN NaN NaN
1 NaN 2.0 NaN NaN
2 -2.0 NaN NaN NaN
3 NaN NaN 1.0 NaN
4 NaN NaN NaN 5.0
5 NaN 8.0 NaN NaN
6 NaN NaN -1.0 NaN

关于python - Pandas groupby : *full* join result of groupwise operation on original index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52132377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com