我正在尝试使用Pandas来获得不同群体的先前值的累积平均值。
我的原始数据框(df)是:
idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Apr-18', 'Apr-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'A', 'B', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
data = [{'xx': 1}, {'xx': 5}, {'xx': 3}, {'xx': 2}, {'xx': 7}, {'xx': 3},{'xx': 1}, {'xx': 6}, {'xx': 3}, {'xx': 5}, {'xx': 2}, {'xx': 3},{'xx': 1}, {'xx': 9}, {'xx': 3}, {'xx': 2}, {'xx': 7}, {'xx': 3}, {'xx': 6}, {'xx': 8}, {'xx': 2}, {'xx': 7}, {'xx': 9}]
df = pd.DataFrame(data, index=idx, columns=['xx'])
df.index.names=['date','type']
df=df.reset_index()
df['date'] = pd.to_datetime(df['date'],format = '%b-%y')
df=df.set_index(['date','type'])
df['xx'] = df.xx.astype('float')
我正在寻找的结果(不同类型的先前值的累积平均值)如下:
date type xx yy
0 2018-01-01 A 1.0 NaN
1 2018-01-01 B 5.0 NaN
2 2018-02-01 B 3.0 5.000000
3 2018-03-01 A 2.0 1.000000
4 2018-03-01 B 7.0 4.000000
5 2018-03-01 C 3.0 NaN
6 2018-04-01 A 1.0 1.500000
7 2018-04-01 B 6.0 5.000000
8 2018-05-01 B 3.0 5.250000
9 2018-06-01 A 5.0 1.333333
10 2018-06-01 B 2.0 4.800000
11 2018-06-01 C 3.0 3.000000
12 2018-07-01 A 1.0 2.250000
13 2018-08-01 B 9.0 4.333333
14 2018-08-01 C 3.0 3.000000
15 2018-09-01 A 2.0 2.000000
16 2018-09-01 B 7.0 5.000000
17 2018-10-01 C 3.0 3.000000
18 2018-10-01 A 6.0 2.000000
19 2018-10-01 B 8.0 5.250000
20 2018-11-01 A 2.0 2.571429
21 2018-12-01 B 7.0 5.555556
22 2018-12-01 C 9.0 3.000000
我尝试了以下Pandas代码,但未成功(滚动操作时出现错误):
df['yy'] = (df.assign(H=(df.groupby('type').xx.transform('cumsum')/(df.groupby('type').xx.cumcount()+1)))).groupby('type').H.rolling(1).apply(lambda x: x[-1])
请注意,代码的第一部分工作正常:
df['yy'] = (df.groupby('type').xx.transform('cumsum')/(df.groupby('type').xx.cumcount()+1))
**如果您可以解决我的错误,或者提出另一种优雅的方法来处理熊猫问题,这将很有用。谢谢!
答案 0 :(得分:1)
我正在使用expanding
df.groupby('type')['xx'].expanding(min_periods=2).mean().\
reset_index(level=0,drop=True).reindex(df.index)
date type
2018-01-01 A NaN
B NaN
2018-02-01 B 4.000000
2018-03-01 A 1.500000
B 5.000000
C NaN
2018-04-01 A 1.333333
B 5.250000
2018-05-01 B 4.800000
2018-06-01 A 2.250000
B 4.333333
C 3.000000
2018-07-01 A 2.000000
2018-08-01 B 5.000000
C 3.000000
2018-09-01 A 2.000000
B 5.250000
2018-10-01 C 3.000000
A 2.571429
B 5.555556
2018-11-01 A 2.500000
2018-12-01 B 5.700000
C 4.200000
Name: xx, dtype: float64
答案 1 :(得分:0)
df['yy'] = (df.assign(H=(df.groupby('type').xx.transform('cumsum')/(df.groupby('type').xx.cumcount()+1)))).groupby('type').H.shift()
然后(使用滚动):
df['yy'] = (df.assign(H=(df.groupby('type').xx.transform('cumsum')/(df.groupby('type').xx.cumcount()+1)))).groupby('type').H.rolling(2).apply(lambda x: x[-2]).reset_index(level=0,drop=True).reindex(df.index)
欢迎其他任何方式!