0

I have a data set in a Pandas data frame with dates like this:

  • 2/3/2020
  • 19/3/2020

And I want to have the dates like this:

  • 3/2/2020
  • 3/19/2020

The data set is called COVID and the column is called 'fecha_not'

This is a sample of my data set:

enter image description here

Parhubix
  • 1
  • 2
  • 1
    would you copy paste your data here please – ombk Dec 03 '20 at 21:19
  • 4
    `df['col'] = pd.to_datetime(df['col'].dt.strftime('%m/%d/%Y'))` ? OR if your data is not in date format then just, `df['col'] = pd.to_datetime(df['col'], dayfirst=True)` – David Erickson Dec 03 '20 at 21:26
  • 1
    David, thank you, that was exactly what I was looking for. Worked perfectly :D – Parhubix Dec 03 '20 at 21:32
  • Is the problem with the display? If the dataframe is able to store the data in date format as dd/mm/yyyy then the dataframe is not an issue but the display is. You can change the display. Review this https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas for more details – Joe Ferndz Dec 03 '20 at 21:38

2 Answers2

1

Use strftime

In [2]: df = pd.DataFrame({'fecha_not' : ['2/3/2020', '6/3/2020', '7/3/2020', '9/3/2020', '9/3/2020'],
   ...: 'Estodo' : "Leve"})
   ...: df
Out[2]: 
  fecha_not Estodo
0  2/3/2020   Leve
1  6/3/2020   Leve
2  7/3/2020   Leve
3  9/3/2020   Leve
4  9/3/2020   Leve

In [3]: df['fecha_not'] = pd.to_datetime(df.fecha_not).dt.strftime('%d/%m/%Y')
   ...: df
Out[3]: 
    fecha_not Estodo
0  03/02/2020   Leve
1  03/06/2020   Leve
2  03/07/2020   Leve
3  03/09/2020   Leve
4  03/09/2020   Leve
Amir saleem
  • 1,404
  • 1
  • 8
  • 11
0

Hello you can use import datetime and apply it to the column, then with .strftime() you can change the format. Check more here

import datetime

x = datetime.datetime(2020, 3, 19)
x.strftime("%m %d %Y")
'03 19 2020'