Reputation: 2717
I have a dataframe like this:
df = pd.DataFrame({'x': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'], 'y': [0,1,0,1,0,1,0,1], 'z':[100, 102, 110, 115, 200, 202, 230, 240]})
x y z
0 a 0 100
1 a 1 102
2 a 0 110
3 a 1 115
4 b 0 200
5 b 1 202
6 b 0 230
7 b 1 240
After this:
df.groupby(['x', 'y'])['z'].sum()
x y
a 0 210
1 217
b 0 430
1 442
I want to find out the difference between sums in percentage for each group. In other words I want this:
(217-210)/210 = 3.33
(442-430)/430 = 2.79
Upvotes: 2
Views: 238
Reputation: 88236
You could groupby
both x
and y
and take the sum
as you already do, and then use DataFrame.pct_change
to take the percentage change having grouped again by x
:
g = df.groupby(['x','y'])['z'].sum()
g.groupby(level=0).pct_change().mul(100).dropna().reset_index(drop=True, level=1)
x
a 3.333333
b 2.790698
Name: z, dtype: float64
Upvotes: 1