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
Difference between rows or columns of a pandas DataFrame object is found using the diff() method. The axis parameter decides whether difference to be calculated is between rows or between columns. When the periods parameter assumes positive values, difference is found by subtracting the previous row from the next row.
subtract() function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs.
Select row 23 and copy it. Right-click the row number of row 24. Select 'Paste Special...' from the context menu. Select Subtract, then click OK.
Pandas dataframe.subtract () function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs. Attention geek!
You could iterate over the dataframe and manually pick each row. That would be pretty straightforward, but not necessarily the best way. While looking around the web for some pointers, I stumbled across this answer that does exactly what I need to do.
Example #1: Use subtract () function to subtract each element of a dataframe with a corresponding element in a series. Let’s use the dataframe.subtract () function for subtraction. Example #2: Use subtract () function to subtract each element in a dataframe with the corresponding element in other dataframe
The output we desire is the following: import pandas as pd s = pd.Series ( [11,15,22,27,36,69,77]) s.diff ().fillna (s) You can use the pythonic shift function. see how I did it.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With