问题
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.
回答1:
I believe need change ordering by iloc[::-1]
:
df1 = (df.iloc[::-1]
.groupby('A', sort=False)
.rolling(7, on='B',min_periods=0).C
.sum()
.iloc[::-1])
回答2:
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]
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
回答3:
I think you want
df["C"] = df["A"].cumsum()
See documentation here
来源:https://stackoverflow.com/questions/50411098/how-to-do-forward-rolling-sum-in-pandas