I have a group like following, how can I know the difference of every observation with it's group minimum value
GROUP VALUE
1 5
2 2
1 10
2 20
1 7
So, my desired output should be like
GROUP VALUE diff
1 5 3
2 2 0
1 10 5
2 20 18
1 7 5
How can I achieve it with the help of pandas
Thanks for all the help
I think you need GroupBy.transform
with subtract:
df['diff'] = df['VALUE'] - df.groupby('GROUP')['VALUE'].transform('min')
print (df)
GROUP VALUE diff
0 1 5 0
1 2 2 0
2 1 10 5
3 2 20 18
4 1 7 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With