【问题标题】:How to convert time string field into "hh:mm" format?如何将时间字符串字段转换为“hh:mm”格式?
【发布时间】:2021-05-12 07:39:07
【问题描述】:

我有这样的熊猫数据框:

import pandas as pd
d = {
         'time': [0, 100, 200, 1400, 1500],
         'value': [9, 8, 7, 6, 5],
         'three': ['a', 'b', 'a', 'a', 'b']
    }
df = pd.DataFrame(d)

想如何将time转换成hh:mm格式得到:00:0001:0002:00

没有if-then怎么办?

【问题讨论】:

  • 时间是否以分钟为单位?
  • @SathyasarathiGunasekaran 可能不会,如果 100 会变成 01:00。似乎它被给出为“连接小时的两位数和分钟的两位数,然后将结果解释为整数”。

标签: python datetime time


【解决方案1】:

您需要将其转换为字符串并执行zfill() 并使用':' 进行连接

d['time'] = [str(a).zfill(4)[:2]+':'+str(a).zfill(4)[2:4] for a in d['time']]

【讨论】:

    猜你喜欢
    • 2021-08-31
    • 2017-03-20
    • 1970-01-01
    • 2018-07-20
    • 2018-06-07
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多