0
我有一個df
:熊貓dataframe rolling_mean()如何做?
sales net_pft
STK_ID RPT_Date
600141 201.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 201.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 201.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 201.166 4.945 # same as original value
20110331 24.445 5.003 # average of row1&row2
.....
如何寫在整潔的熊貓表達?
這對我來說並不清楚你想要什麼?你的意思是某種「累積平均值」 – joris