1

i have a csv file and want to select one specific colum (date string). then i want to change the format of the date string from yyyymmdd to dd.mm.yyyy for every entry.

i read the csv file in a dataframe with pandas and then saved the specific column with the header DATE to a variable.

import pandas as pd

# read csv file
df = pd.read_csv('csv_file')

# save specific column
df_date_col = df['DATE']

now i want to change the values in df_date_col. How can i do this? I know i can do it a step before like this: df['DATE'] = modify(df['DATE'])

Is this possible just using the variable df_date_col?

If i try df_date_Col['DATE']=... it will give a KeyError.

bucky
  • 392
  • 4
  • 18

1 Answers1

1

Use to_datetime with Series.dt.strftime:

df['DATE'] = pd.to_datetime(df['DATE'], format='%Y%m%d').dt.strftime('%d.%m.%Y')

Is this possible just using the variable df_date_col?

Sure, but working with Series, so cannot again select by []:

df_date_col = df['DATE']
df_date_col = pd.to_datetime(df_date_col, format='%Y%m%d').dt.strftime('%d.%m.%Y')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252