0

If I have a dataframe created from a csv file containing a column with date values (11.01.2023) how can I transform the column into a date format like dd/mm/yyyy (day 2 characters, month 2 characters, years 4 characters)? Thank you all

import pandas as pd

df = pd.DataFrame({'Date': {0: '11.01.2023 ', 1: '22.01.2023', 2: '08.02.2023' }, 
                  'Price':{0:123, 1:456, 2:789}})
df
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
Frank
  • 3
  • 2
  • 1
    Simply `df['Date'].str.replace('.', '/', regex=False)` or `pd.to_datetime(df['Date'], dayfirst=True).dt.strftime('%d/%m/%Y')` – mozway Mar 30 '23 at 12:51
  • Thanks for the reply. I did df['Date'].str.replace('.', '/', regex=False) and the dataframe results as at the beginning – Frank Mar 30 '23 at 14:32
  • Did you assign? `df['Date'] = df['Date'].str.replace('.', '/', regex=False)` – mozway Mar 30 '23 at 14:41
  • Yes. I also tried with the command df['Date'] = df['Date'].str.replace('.', '/', regex=False) But if I check the properties: df.info() I get the column Date is defined as "object" while I thought it should be of type "datetime64[ns]". Is that correct? – Frank Mar 30 '23 at 15:04
  • Then provide a reproducible example, there is no issue with your current one. – mozway Mar 30 '23 at 18:17
  • HI. These are the steps I take : – Frank Mar 31 '23 at 06:50
  • how can i attach a small sample file? – Frank Mar 31 '23 at 06:54
  • Provide a minimal reproducible example as [edit] to your question: `df.head().to_dict('list')` – mozway Mar 31 '23 at 07:33
  • import pandas as pd df = pd.DataFrame({'Date': {0: '11.01.2023 ', 1: '22.01.2023', 2: '08.02.2023' }, 'Price':{0:123, 1:456, 2:789}}) – Frank Mar 31 '23 at 08:12

0 Answers0