簡體   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