Difference in Python values ​​in a data frame by group key

I have a DataFrame

name   value
A       2
A       4
A       5
A       7
A       8
B       3
B       4
B       8
C       1
C       3
C       5 

And I want to get a difference of values ​​based on each name like this

name   value   dif
A       2      0
A       4      2
A       5      1
A       7      2
A       8      1
B       3      0
B       4      1
B       8      4
C       1      0
C       3      2
C       5      2

Can someone show me the easiest way?

+4
source share
1 answer

You can use GroupBy.diffto calculate the difference between consecutive lines on a grouped object. Optionally, fill in the missing values ​​(the first line in each group) by 0 and their final end in the form of integers.

df['dif'] = df.groupby('name')['value'].diff().fillna(0).astype(int)
df

enter image description here

+4
source

Source: https://habr.com/ru/post/1663054/


All Articles