[英]Removing character from string in dataframe
I have a dataframe, one column of which is filled with entries like this: 我有一个数据框,其中一栏充满了这样的条目:
2017-03-01T09:30:00.436
2017-03-01T09:30:00.444
...
Is there a way to convert the entire column into datetime format? 有没有办法将整个列转换为日期时间格式?
So far I have tried using 到目前为止,我已经尝试使用
str.replace('T',' ') over iterrows()
as well as slicing methods but neither seems to work. 以及切片方法,但似乎都不起作用。 Any help will be greatly appreciated.
任何帮助将不胜感激。 Thanks!
谢谢!
Use regex=True
for replace
substrings: 使用
regex=True
replace
子字符串:
df['col'] = df['col'].replace('T', ' ', regex=True)
But maybe need only to_datetime
: 但也许只需要
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]
to_datetime()
应该可以将其转换为日期时间格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.