简体   繁体   中英

how to i change the format of date from dd-mm-yyyy to dd/mm/yyyy in a csv file

image

I have CSV with date in this format which is to be changed? how can I do that?

import pandas as pd

# I have taken an example. You could do a pd.read_csv(filename) to read from file

#Input in dd-mm-yyyy format
df = pd.DataFrame({'DOB': {0: '26-01-2016', 1: '26-01-2016'}})

#Convert to pandas datetime object
df['DOB'] = pd.to_datetime(df.DOB)

#Convert to dd/mm/yyyy format('%d/%m/%Y')
df['DOB'] = df['DOB'].dt.strftime('%d/%m/%Y')

You can read more here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.strftime.html

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