Reputation: 477
I have following panda data-frame and I am trying to group it by Index1 column values after extracting the right hand side of the dash sign, i.e., z and y.
Index1 Index2 Value
a-z 1 10
a-z 2 10
a-z 1 4
c-z 2 5
c-z 1 9
c-z 2 6
a-z 1 6
a-y 2 7
b-y 1 8
b-y 2 2
c-y 1 9
c-y 2 4
So the final result after using group-by sum should be something like this:
Index1 Index2 Value
z 1 29
z 2 21
y 1 17
y 2 13
Thanks a lot for your help.
Upvotes: 1
Views: 74
Reputation: 8631
You can use:
df.groupby([df['Index1'].str[-1], df['Index2']])['Value'].sum().reset_index()
Output:
Index1 Index2 Value
0 y 1 17
1 y 2 13
2 z 1 29
3 z 2 21
Upvotes: 2