【问题标题】:How to get average time from list of times如何从时间列表中获取平均时间
【发布时间】:2019-05-31 07:41:54
【问题描述】:

您好,我对从时间列表到每次测试的平均时间有疑问。

输出:

test1 ['0:02:30.000000', '0:02:28.000000', '0:02:31.000000', '0:02:33.000000', '0:02:34.000000', '0:02: 37.000000'、'0:02:30.000000'、'0:02:30.000000']

test2 ['0:23:25.789000', '0:23:04.653000']

test3 ['0:32:21.873000', '0:32:33.752000', '0:33:30.383000', '0:32:29.835000']

预期结果:

test_name = 平均时间 例如:

test2 = 0:23:15:442000

我尝试将 str 转换为时间:

time == dt.strptime(str(time), '%H:%M:%S.%f')

但是我不能对所有时间求和然后除以列表的长度。

你能帮我解决这个问题吗?

def average_time(file_path):
    df = pd.DataFrame(pd.read_excel(file_path))
    test_names_list = collections.Counter(df.iloc[:, 0])
    times_list = {}
    for test_name in test_names_list:
        times_list[test_name] = []
        for row in range(df.shape[0]):
            if str(df.iloc[row, 0]) == test_name:
                times_list[test_name].append(str(df.iloc[row, 5]))
        print(test_name, times_list[test_name])

【问题讨论】:

标签: python list datetime average


【解决方案1】:

已解决。

def average_time(file_path):
    df = pd.DataFrame(pd.read_excel(file_path))
    test_names_list = collections.Counter(df.iloc[:, 0])
    times_list = {}
    for test_name in test_names_list:
        times_list[test_name] = []
        for row in range(df.shape[0]):
            if str(df.iloc[row, 0]) == test_name:
                times_list[test_name].append(get_sec(str(df.iloc[row, 5])))
        print(test_name, str(dt.timedelta(seconds=mean(times_list[test_name]))))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多