Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python value difference in dataframe 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 the value differences 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 anyone show me the easiest way?

like image 672
jimmy15923 Avatar asked Dec 07 '16 07:12

jimmy15923


People also ask

How do you get Groupby index in pandas?

How to perform groupby index in pandas? Pass index name of the DataFrame as a parameter to groupby() function to group rows on an index. DataFrame. groupby() function takes string or list as a param to specify the group columns or index.

What does Group_by do in pandas?

groupby() function is used to split the data into groups based on some criteria. pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. sort : Sort group keys.

How do you find the difference between two values in pandas?

During data analysis, one might need to compute the difference between two rows for comparison purposes. This can be done using pandas. DataFrame. diff() function.

Does Groupby order matter pandas?

Groupby preserves the order of rows within each group. Unfortunately the answer to this question is NO.


1 Answers

You can use GroupBy.diff to compute the difference between consecutive rows per grouped object. Optionally, filling missing values( first row in every group) by 0 and casting them finally as integers.

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

enter image description here

like image 80
Nickil Maveli Avatar answered Oct 23 '22 03:10

Nickil Maveli