繁体   English   中英

从 csv 文件将 HH:MM:SS 转换为 python 中的秒

[英]Converting HH:MM:SS to seconds in python from a csv file

我想把它转换成秒

[44206 rows x 30 columns]

    0        00:00:00
    1        00:00:10
    2        00:00:00
    3        00:00:20
    4        00:00:22
      
    44201    01:51:02
    44202    01:51:12
    44203    01:51:22
    44204    01:51:32
    44205    01:51:42
    Name: Cumulative Time, Length: 44206, dtype: object

为此,我使用了以下代码

def step_time(x):
    time_list=x.split(":")
    print (time_list)
    time = (3600 * float(time_list[-3]) + 60 * float(time_list[-2]) + float(time_list[-1]))
    return time

当我使用此代码时,我收到错误消息“AttributeError: 'Series' object has no attribute 'split'”

然后我将x.split(":")更改为x.str.split(":")

然后我得到 time_list 为:

[44206 rows x 30 columns]
0        [00, 00, 00]
1        [00, 00, 10]
2        [00, 00, 00]
3        [00, 00, 20]
4        [00, 00, 22]
    
44201    [01, 51, 02]
44202    [01, 51, 12]
44203    [01, 51, 22]
44204    [01, 51, 32]
44205    [01, 51, 42]
Name: Cumulative Time, Length: 44206, dtype: object

现在看起来我在一个数组中有一个数组不知道接下来我应该做什么。

利用:

df['New'] = pd.to_timedelta(df['Cumulative Time']).dt.total_seconds()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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