python-来自groupby的Pandas累积差异
作者:互联网
我需要从MultiIndex级别的开始算起差,从级别的开始算出衰减.我的示例输入和输出将如下所示:
values
place time
A a 120
b 100
c 90
d 50
B e 11
f 12
g 10
h 9
values
A a NaN
b -20
c -30
d -70
B e Nan
f +1
g -1
h -2
我可以使用grouby获取级别中连续单元之间的差异:
df.groupby(level=0)['values'].diff()
但这不是我想要的!
las,这个被接受的答案不是我想要的.我有一个更好的例子:
arrays = [np.array(['bar', 'bar', 'bar', 'foo', 'foo', 'foo']),
np.array(['one', 'two', 'three', 'one', 'two', 'three'])]
df = pd.DataFrame([1000, 800, 500, 800, 400, 200], index=arrays)
bar one 1000
two 800
three 500
foo one 800
two 400
three 200
expected_result = pd.DataFrame([Nan, -200, -500, Nan, -400, -600], index=arrays)
bar one Nan
two -200
three -500
foo one Nan
two -400
three -600
但是df.groupby(level = 0).diff().cumsum()的结果为:
pd.DataFrame([Nan, -200, -500, Nan, -900, -1100], index=arrays)
bar one Nan
two -200
three -500
foo one Nan
two -900
three -1100
解决方法:
您是否正在寻找积金?
df.groupby(level=0)['values'].diff().cumsum()
标签:pandas-groupby,pandas,dataframe,python,group-by 来源: https://codeday.me/bug/20191025/1926804.html