pandas calculate diffs between rows

You can do it like

df['diff'] = df.groupby('state')['total'].diff()
df

Out:

  state  total  week  diff
0    CA      2    10   NaN
1    UT      7    10   NaN
2    CA     14    11  12.0
3    UT     18    11  11.0
4    CA     21    12   7.0
5    UT     30    12  12.0

Since pandas 0.24 you can use nullable int types but it’s not commonly used

df['diff'] = df.groupby('state')['total'].diff().astype(pd.Int64Dtype())
df

Out:

  state  total  week  diff
0    CA      2    10  <NA>
1    UT      7    10  <NA>
2    CA     14    11    12
3    UT     18    11    11
4    CA     21    12     7
5    UT     30    12    12

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top