1

A pandas DataFrame (df3) has contained two columns contain timedelta64[ns] as shown. How can you calculate the difference time of them in seconds in a new column?

[In][1] df3.head()
Out[1] 

Subset of df3

The new df3 should be like:

exactly what I needed!

how do I get difference in total seconds?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
M_Fatih89
  • 49
  • 8
  • Does this answer your question? [Calculate Pandas DataFrame Time Difference Between Two Columns in Hours and Minutes](https://stackoverflow.com/questions/22923775/calculate-pandas-dataframe-time-difference-between-two-columns-in-hours-and-minu) – Trenton McKinney Aug 23 '20 at 01:21

1 Answers1

4

We can use total_seconds

(df.dropoff_datetime-df.pickup_datetime).dt.total_seconds()
Out[514]: 
0    1327.0
1    2040.0
2    1680.0
3    1975.0
4    3083.0
dtype: float64
df['diff']= (df.dropoff_datetime-df.pickup_datetime).dt.total_seconds()
BENY
  • 317,841
  • 20
  • 164
  • 234