从数据帧中的字符串中删除字符

时间:2017-10-03 12:12:46

标签: python pandas

我有一个数据框,其中一列填充了这样的条目:

2017-03-01T09:30:00.436
2017-03-01T09:30:00.444
...

有没有办法将整个列转换为日期时间格式?

到目前为止,我已尝试使用

str.replace('T',' ') over iterrows()

以及切片方法,但似乎都不起作用。任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:3)

regex=True用于replace子字符串:

df['col'] = df['col'].replace('T', ' ', regex=True)

但也许只需要to_datetime

df = pd.DataFrame({'col':['2017-03-01T09:30:00.436','2017-03-01T09:30:00.444']})

df['col'] = pd.to_datetime(df['col'])
print (df['col'])
0   2017-03-01 09:30:00.436
1   2017-03-01 09:30:00.444
Name: col, dtype: datetime64[ns]

答案 1 :(得分:0)

to_datetime()应该将其转换为日期时间格式