infinite-rotations
infinite-rotations

Reputation: 97

Groupby cumulative operations in successive rows pandas

I want to find the rolling sum and rolling max for the column B for the same values in A in df

df = pd.DataFrame({'A': ['a', 'a', 'a', 'b', 'b', 'b', 'b'], 'B': [5, 2, 4, 7, 1, 11, 3]})

df_result = pd.DataFrame({'A': ['a', 'a', 'a', 'b', 'b', 'b', 'b'], 'B': [5, 2, 4, 7, 1, 11, 3], 'SUM': [5, 7, 11, 7, 8, 19, 22], 'MAX': [5, 5, 5, 7, 7, 11, 11]})

Upvotes: 2

Views: 608

Answers (1)

jezrael
jezrael

Reputation: 862841

Use groupby with aggregation by agg and functions cummax and cumsum, last join to original:

d = {'cummax':'max', 'cumsum':'sum'}
df_result = df.join(df.groupby('A')['B'].agg(['cummax','cumsum']).rename(columns=d))
print (df_result)
   A   B  max  sum
0  a   5    5    5
1  a   2    5    7
2  a   4    5   11
3  b   7    7    7
4  b   1    7    8
5  b  11   11   19
6  b   3   11   22

If is possible modify original DataFrame:

df[['max','sum']] = df.groupby('A')['B'].agg(['cummax','cumsum'])
print (df)
   A   B  max  sum
0  a   5    5    5
1  a   2    5    7
2  a   4    5   11
3  b   7    7    7
4  b   1    7    8
5  b  11   11   19
6  b   3   11   22

Upvotes: 3

Related Questions