Adrian Y
Adrian Y

Reputation: 414

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!

Upvotes: 3

Views: 6828

Answers (2)

jgummersall
jgummersall

Reputation: 31

to_datetime() should work in converting it to datetime format

Upvotes: 0

jezrael
jezrael

Reputation: 863531

Use regex=True for replace substrings:

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

But maybe need only 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]

Upvotes: 4

Related Questions