I have Pandas dataframe with string column which contains timestamp as "20000530 172700" How to change elegantly such string to "2000-05-30 17:27:00" ? Dataframe contains > 10k rows. I don't want take each value,insert "-" and ":" to specified positions. Is there a solution using mask?
1 Answer
you can use pandas.to_datetime:
pd.to_datetime(df["my_column"])
If you want to customize it, you can use pandas.Series.dt.strftime, e.g.:
pd.to_datetime(df["my_column"]).dt.strftime('%d%b%Y')
#The format will be something like 30May2000
You can check all the datetime format codes here.
1 Comment
harp1814
Thanks, Pablo! Just pd.to_datetime(df["my_column"]) gave me wished result. Thanks again!
.datetime(). The format your strings are currently in might make it more difficult though.