1EnemyLeft
1EnemyLeft

Reputation: 959

Subtract next row use current row, python dataframe

I have this Pandas Dataframe in python, I want to know the time difference between two rows, next row subtract previous row, how should I approach this?

Reviewed[x] - Reviewed[x-1] x is the number of where that row is

   ReporterID        ID      Type                   Reviewed
0     27545252  51884791  ReportID 2015-01-14 00:00:42.640000
1     29044639  51884792  ReportID 2015-01-14 00:00:02.767000
2     29044639  51884793  ReportID 2015-01-14 00:00:28.173000
3     29044639  51884794  ReportID 2015-01-14 00:00:46.300000
4     27545252  51884796  ReportID 2015-01-14 00:01:54.293000
5     29044639  51884797  ReportID 2015-01-14 00:01:17.400000
6     29044639  51884798  ReportID 2015-01-14 00:01:57.600000

Upvotes: 9

Views: 9468

Answers (1)

elyase
elyase

Reputation: 40963

Assuming your Reviewed column are datetimes you can do:

df.ix[4, 'Reviewed'] - df.ix[3, 'Reviewed']

If you want to do it for all rows at once:

df['Reviewed'] - df['Reviewed'].shift()

Upvotes: 12

Related Questions