我有一个df
:
sales net_pft
STK_ID RPT_Date
600141 20101231 46.780 1.833
20110331 13.725 0.384
20110630 32.733 1.132
20110930 50.386 1.923
20111231 65.685 2.325
20120331 21.088 0.656
20120630 46.952 1.591
600809 20101231 30.166 4.945
20110331 18.724 5.061
20110630 28.948 6.586
20110930 35.637 7.075
20111231 44.882 7.805
20120331 22.140 4.925
20120630 38.157 7.868
我想对所有列进行滚动平均,在 groupby 之后STK_ID
,伪代码表示的规则如下:
if RPT_Date[4:8] == '0331':
all_column = rolling_mean(all_column,2)
if RPT_Date[4:8] == '0630':
all_column = rolling_mean(all_column,3)
if RPT_Date[4:8] == '0930':
all_column = rolling_mean(all_column,4)
if RPT_Date[4:8] == '1231':
all_column = rolling_mean(all_column,5)
if is_the_first_row():
keep_original_values()
all_column
这里代表' sales
','net_pft'
。最终结果如下:
sales net_pft
STK_ID RPT_Date
600141 20101231 46.780 1.833 # same as original value
20110331 30.253 1.109 # average of row1&row2
20110630 31.079 1.116 # average of row1&row2&row3
......
600809 20101231 30.166 4.945 # same as original value
20110331 24.445 5.003 # average of row1&row2
.....
如何写出整洁的 Pandas 表达式?