Cheng
Cheng

Reputation: 206

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"

Upvotes: 1

Views: 88

Answers (1)

jezrael
jezrael

Reputation: 862641

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

Upvotes: 4

Related Questions