Skip to main content
 首页 » 编程设计

python之在 Pandas 中将字符串转换为日期时间

2024年10月24日25xiaohuochai

我在 pandas 数据框中有一列,格式为 20151028 193133 我想将此字符串转换为日期时间格式,如 2015-10-28 19:26:48

我该怎么做?

请您参考如下方法:

尝试 to_datetime和参数 format docs :

print df    
               col 
0  20151028 193133 
1  20151028 193133 
2  20151028 193133 
 
df['col'] = pd.to_datetime(df['col'], format="%Y%m%d %H%M%S") 
print df 
                  col 
0 2015-10-28 19:31:33 
1 2015-10-28 19:31:33 
2 2015-10-28 19:31:33