Skip to content Skip to sidebar Skip to footer

Calculate Difference Between Cells In Different Rows In A Pandas Dataframe

I have a dataframe in pandas like this: Timestamp ID X X Diff Y Y Diff 0 0 100 1.728 None 14.378 None 1 12 100 2.035 None 1

Solution 1:

Groupby 'ID' and calculate difference and then assign back to df:

df[['X diff','Y Diff']]=df.groupby('ID')[['X','Y']].diff()

output of df:

  Timestamp     ID      X        Y      X diff  Y Diff
001001.72814.378NaNNaN1121002.03514.3780.3070.0002241002.34214.3780.3070.0003361002.63014.3780.2880.0004481002.93714.4160.3070.038

Post a Comment for "Calculate Difference Between Cells In Different Rows In A Pandas Dataframe"