M_Fatih89
M_Fatih89

Reputation: 49

How to calculate the time (in seconds) difference between two DateTime columns using pandas?

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?

Upvotes: 2

Views: 869

Answers (1)

BENY
BENY

Reputation: 323236

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()

Upvotes: 5

Related Questions