【问题标题】:Converting Excel cells to time将 Excel 单元格转换为时间
【发布时间】:2020-08-25 02:14:13
【问题描述】:

我正在尝试将时间字符串列表((?)不确定,尽管 type (list[1]) 表示 str),转换为 timedate(或者任何其他更好的替代方法)。

如果满足某个条件,我想总结一下这些时间。这是我到目前为止得到的:

import os
import csv
import datetime
import pandas as pd

read_file = pd.read_excel ('test.xlsx')
read_file2 = read_file['Duration']

uren_lijst = []

for row in read_file2:
    for row in read_file2:
    uren_lijst.append(row)

我已经尝试过使用 'pd.to_datetime' 方法,但这似乎会创建时间戳,而且我只需要时间而且我对日期本身不感兴趣。

【问题讨论】:

    标签: python python-3.x pandas list


    【解决方案1】:

    你可以试试这个:

    首先将其转换为datetime 列。然后从中提取hour

    read_file['Duration'] = pd.to_datetime(read_file['Duration'])
    hours = read_file['Duration'].dt.hour
    

    在 OP 的评论之后:

    time = read_file['Duration'].dt.time
    

    【讨论】:

    • 这几乎可以工作,除了分钟也是必要的。所以我还需要最后一个完整小时之后的时间总和。
    • 你能分享你需要的确切输出吗?
    • “dt.time”功能完成了这项工作,感谢您的“dt.hour”功能,我能够找到它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多