Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

python - Pandas: Convert String column to timestamp

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?

question from:https://stackoverflow.com/questions/65909099/pandas-convert-string-column-to-timestamp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...