简体   繁体   中英

Converting string to datetime object in pandas

I have a column called 'SubmitTime' which is a string per observation. An example would be: 'Wed Apr 12 14:42:23 PDT 2017'

I need to sort this dataframe based on submission time (the ones that submitted first, are on top). How can I convert this column into datetime and sort the dataframe in Pandas?

Assuming you dataframe is df

df.iloc[pd.to_datetime(df.SubmitTime).argsort()]

This leaves your dataframe intact, 'SubmitTime' remains strings

Otherwise, I'd convert 'SubmitTime' to datetime and sort

df.assign(SubmitTime=pd.to_datetime(df.SubmitTime)).sort_values('SubmitTime')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM