熊猫Groupby pct_change没有垫

时间:2018-11-08 17:02:55

标签: python pandas pandas-groupby

进行百分比更改,除填充我尝试设置pct_change(fill_method = None)的填充外,其他所有方法都工作正常,但出现错误:

filled = getattr(self,fill_method)(limit = limit)

TypeError:getattr():属性名称必须为字符串

i按名称分组,并期望每个名称的前3个值为nan。但是,它将所有值都视为列表的一部分,并对所有值计算pct_change。在示例中,此方法适用于foo,但不适用于其他名称。请参见下面的代码。

import pandas as pd
import numpy as np


np.random.seed(7) 
df = pd.DataFrame(np.random.randn(40, 1))

a=10*('foo',)
b=10*('bar',)
c=10*('laa',)
d=10*('loo',)
df['names']=a+b+c+d

df['percentage_change']=df.groupby('names')[0].pct_change(periods=3)

我得到什么:

names      pct change
foo        nan
foo        nan
foo        nan
foo        -.7589
foo       .693195
...       ....
bar       -.6435
bar       -1.16857
bar       -.158
bar       -.582117

我期望的是

names      pct change
foo        nan
foo        nan
foo        nan
foo        -.7589
foo       .693195
...       ....
bar       nan
bar       nan
bar       nan
bar       -.582117

1 个答案:

答案 0 :(得分:1)

这是known issue in pandas 0.23

一种解决方法是使用apply

df['percentage_change'] = df.groupby('names')[0].apply(lambda x: x.pct_change(periods=3))

           0 names  percentage_change
0   1.690526   foo                NaN
1  -0.465937   foo                NaN
2   0.032820   foo                NaN
3   0.407516   foo          -0.758941
4  -0.788923   foo           0.693195
5   0.002066   foo          -0.937064
6  -0.000890   foo          -1.002185
7  -1.754724   foo           1.224202
8   1.017658   foo         491.675907
9   0.600499   foo        -675.425038
10 -0.625429   bar                NaN
11 -0.171548   bar                NaN
12  0.505299   bar                NaN
13 -0.261356   bar          -0.582117
14 -0.242749   bar           0.415048
15 -1.453241   bar          -3.876001
16  0.554580   bar          -3.121931
...