Pandas Converting an object into timedelta

I have the following data

   Duration
0  00:00:00  
1  00:00:00  
2  00:00:57  
3  00:03:16  
4  00:00:00  

And Duration is stored as an object. I would like to convert this into an integer having seconds. for eg 00:03:16 gets converted into 196. I tried various things like astype(timedelta64[s]) but no success. I tried extracting the minutes and seconds and tried converting to integer, that also did not yield results. I am unable to convert the extracted string into an integer.

Upvotes: 5

Views: 4973

Answers (1)

su79eu7k
su79eu7k

Reputation: 7326

in case of series,

s.map(lambda x: pd.to_timedelta(x).seconds)

dataframe,

df.applymap(lambda x: pd.to_timedelta(x).seconds)

Upvotes: 3

Related Questions