需要将时间转换为h:m:s

时间:2019-10-03 22:02:56

标签: python pandas

我的时间格式缺少小时,例如36:21,或者时间格式不完整,例如1:23:30,想将其转换为标准时间格式,例如00:00:00, 但是我不知道为什么我的代码不起作用。

需要转换时间格式H:M:S这样-> 00:00:00 len(x)== 6即36:21; len(x)== 7,即1:23:30

想要获得00:36:21和01:23:30

for x in df7[' Chip Time']:
    if len(x) == 6: 
        x = '00:'+ x
    elif len(x) == 7:
        x = '0'+ x
    print (df7[' Chip Time']) 

================================================ ============ 需要支持。谢谢!

1 个答案:

答案 0 :(得分:0)

您应该使用apply方法,它比迭代快得多

我也建议您阅读https://thispointer.com/python-how-to-pad-strings-with-zero-space-or-some-other-character/

这应该有效:

df7[' Chip Time'] = df7[' Chip Time'].apply(lambda x : x.rjust(5, '0').rjust(6, ':').rjust(8, '0'))

相关问题