如何使用熊猫一起添加两个时间列

时间:2019-03-31 16:13:29

标签: python pandas

我正在尝试将MM:SS格式的两列加在一起。我尝试使用to_timedelta,但出现错误“预期的hh:mm:ss格式”

1 个答案:

答案 0 :(得分:4)

to_timedelta之前的零小时内将00:添加到两列:

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