pandas 如何在熊猫中进行前滚求和?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50411098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do forward rolling sum in pandas?
提问by Moses Soleman
I have this dataframe:
我有这个数据框:
dates = pd.date_range(start='2016-01-01', periods=20, freq='d')
df = pd.DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8,
'B': np.concatenate((dates, dates)),
'C': np.arange(40)})
I sorted the data frame by Date:
我按日期对数据框进行了排序:
df.sort_values('B',inplace=True)
I am looking to do a forward rolling sum on date. However, I can only do backward rolling sum using:
我希望在日期上做一个向前滚动的总和。但是,我只能使用以下方法进行向后滚动求和:
df.groupby('A').rolling(7, on='B',min_periods=0).C.sum()
A B
1 2016-01-01 0.0
2016-01-02 1.0
2016-01-03 3.0
2016-01-04 6.0
2016-01-05 10.0
2016-01-06 15.0
I want to do forward rolling sum.
我想做向前滚动总和。
回答by jezrael
I believe need change ordering by iloc[::-1]
:
我相信需要通过iloc[::-1]
以下方式更改订购:
df1 = (df.iloc[::-1]
.groupby('A', sort=False)
.rolling(7, on='B',min_periods=0).C
.sum()
.iloc[::-1])
回答by piRSquared
Setup
设置
dates = pd.date_range(start='2016-01-01', periods=20, freq='d')
df = pd.DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8,
'B': np.concatenate((dates, dates)),
'C': np.arange(40)})
Sort by 'B'
then when we roll, roll the reverse with iloc[::-1]
'B'
当我们滚动时按那时排序,反向滚动iloc[::-1]
def rev_roll(x):
return x.iloc[::-1].rolling(7, min_periods=0).sum().iloc[::-1]
df.assign(Roll=df.sort_values('B').groupby('A').C.transform(rev_roll))
Output
输出
A B C Roll
0 1 2016-01-01 0 21
1 1 2016-01-02 1 28
2 1 2016-01-03 2 35
3 1 2016-01-04 3 42
4 1 2016-01-05 4 49
5 1 2016-01-06 5 56
6 1 2016-01-07 6 63
7 1 2016-01-08 7 70
8 1 2016-01-09 8 77
9 1 2016-01-10 9 84
10 1 2016-01-11 10 91
11 1 2016-01-12 11 98
12 1 2016-01-13 12 105
13 1 2016-01-14 13 112
14 1 2016-01-15 14 99
15 1 2016-01-16 15 85
16 1 2016-01-17 16 70
17 1 2016-01-18 17 54
18 1 2016-01-19 18 37
19 1 2016-01-20 19 19
20 2 2016-01-01 20 161
21 2 2016-01-02 21 168
22 2 2016-01-03 22 175
23 2 2016-01-04 23 182
24 2 2016-01-05 24 189
25 2 2016-01-06 25 196
26 2 2016-01-07 26 171
27 2 2016-01-08 27 145
28 2 2016-01-09 28 118
29 2 2016-01-10 29 90
30 2 2016-01-11 30 61
31 2 2016-01-12 31 31
32 3 2016-01-13 32 245
33 3 2016-01-14 33 252
34 3 2016-01-15 34 219
35 3 2016-01-16 35 185
36 3 2016-01-17 36 150
37 3 2016-01-18 37 114
38 3 2016-01-19 38 77
39 3 2016-01-20 39 39
回答by Idan Richman
If your dates aren't fully continuous (like you've got a missing day or two here and there) and you want a fixed N-Days window (and not N-records window), you can use:
如果您的日期不完全连续(就像您在这里和那里缺少一两天)并且您想要一个固定的 N 天窗口(而不是 N 记录窗口),您可以使用:
def forward_rolling_mean(sub_df, col='units', days_ahead=7):
rolling_data = [sub_df[sub_df['date'].between(date+pd.Timedelta(days=1), date+pd.Timedelta(days=1+days_ahead-1))][col].mean() for date in sub_df['date']]
return pd.DataFrame({'%s_next%idays_mean' % (col, days_ahead): rolling_data}, index=sub_df['date'])
you can also change it to return a series instead of dataframe. later on you can join this to the original data.
您还可以更改它以返回一个系列而不是数据框。稍后您可以将其加入原始数据。