i have this column named: temps in dataframe named :df
0 2022-03-18 14:50:11
1 2022-03-18 14:50:00
2 2022-03-18 14:49:59
3 2022-03-18 14:48:00
4 2022-03-18 14:40:00
information about my dataframe df
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 172 entries, 0 to 171
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 temps 172 non-null datetime64[ns]
1 id 172 non-null int64
i need to subtract each two succéssive row and get the results with seconds
how can i do that ?
CodePudding user response:
Use .view(int)
to convert the dates to numbers, .div(1e9)
to convert the resulting nanoseconds into seconds, and .diff()
to compute the difference between each successive row:
df['temps'] = df['temps'].view(int).div(1e9).diff().fillna(0)
Output:
>>> df
temps
0 0.0
1 -11.0
2 -1.0
3 -119.0
4 -480.0