简体   繁体   中英

How do I add two time column together using pandas

I am trying to add two column together in the format of MM:SS. I tried to use to_timedelta but there was an error "expected hh:mm:ss format"

Add 00: to both columns for zero hours before to_timedelta :

df = pd.DataFrame({'col1':['10:20','32:32','21:10'], 
                   'col2':['13:56','22:02','01:30']})

df['new'] = pd.to_timedelta('00:' + df['col1']) + pd.to_timedelta('00:' + df['col2'])
print (df)
    col1   col2      new
0  10:20  13:56 00:24:16
1  32:32  22:02 00:54:34
2  21:10  01:30 00:22:40

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