当 Hour/Minute/Second 是 python 或 pandas 中的单个数字时,有谁知道如何正确格式化时间

Does anyone know how to get the time to format correctly when the Hour/Minute/Second is a SINGLE digit in python or pandas

我数据框中的某些行的时间为“13:2:7”,我希望它们为“13:02:07”。

我已尝试将 pd.to_datetime 应用到该列,但它不起作用

有人可以建议一些方法来将时间格式化为标准格式

找到解决方案

我通过pandasto_timedelta

解决了

pd.to_timedelta("13:2:3") 或者通过它传递dataframe的列

结果:

"13:02:03"

ps:您将得到结果“0 天 13:02:03”

为了删除0天

df["column_name"].astype(str).map(lambda x: x[7:]))

我们将切片出现的初始 0 天字符串

注意:最终结果将以字符串形式出现

如果你想要时间对象,

使用 datetime 模块中的 pandas pd.to_datetimestrftime