简体   繁体   中英

How do I format date using pandas?

My data 'df' shows data 'Date' as 1970-01-01 00:00:00.019990103 when this is formatted to date_to using pandas. How do I show the date as 01/03/1999?

consider LoneWanderer's comment for next time and show some of the code that you have tried.

I would try this:

from datetime import datetime

now = datetime.now()

print(now.strftime('%d/%m/%Y'))

You can print now to see that is in the same format that you have and after that is formatted to the format required.

I see that the actual date is in last 10 chars of your source string.

To convert such strings to a Timestamp (ignoring the starting part), run:

df.Date = df.Date.apply(lambda src: pd.to_datetime(src[-8:]))

It is worth to consider to keep this date just as Timestamp , as it simplifies operations on date / time and apply your formatting only in printouts.

But if you want to have this date as a string in "your" format, in the "original" column, perform the second conversion ( Timestamp to string ):

df.Date = df.Date.dt.strftime('%m/%d/%Y')

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