简体   繁体   中英

Convert an object datatype column with format mm:ss to a time format pandas

I have a dataframe that has an column that has an object datatype with the format mm:ss. I want to convert that column to a time format so that I could turn the time into seconds instead of mm:ss. However, I have not been able to convert the column into a time format.

Example of my data:

time
33:22
24:56
30:15
26:57

I have tried:

df['time'] = pd.to_timedelta(df['time'])

How do I convert this object data type column to a time format? And ultimately to total seconds?

Just add '00:' to the beginning.

df['time'] = pd.to_timedelta('00:' + df['time'])
df['total seconds'] = df['time'].dt.total_seconds()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM