0

After importing data from an Excel file I get the Following output:

import pandas as pd
import numpy as np


#On récupère le fichier Excel
df = pd.read_excel("C:\\Users\\YannickLECROART\\Desktop\\comedie.xlsx", skiprows=1)

series_comm = df.iloc[:, 1:3] 

enter image description here

But what I would like to do now is to change the datetime format to get the Following result in my date colum:

YYYY-MM-DD HH:MM:SS

instead of YYYY-MM-DD HH:MM:SS.0000000 from the originated file

The purpose is Simply to get rid of all those zeros as you can see.

Thanks in advance.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Yannick
  • 399
  • 3
  • 10
  • 16

1 Answers1

1

You can use pd.to_datetime function:

In [1028]: df['date'] = df['date'].apply(pd.to_datetime)
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • I get an error: Traceback (most recent call last): File "C:\Users\YannickLECROART\Miniconda3\envs\machinelearning\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc return self._engine.get_loc(key) KeyError: 'date' – Yannick Nov 29 '18 at 10:23
  • Please use the correct column name for the date column in your dataframe. I used `date`, you should replace it with yours. – Mayank Porwal Nov 29 '18 at 10:24
  • parking places_occupees date COME 238 2017-01-01 00:00:00.0000000 COME 238 2017-01-01 00:01:00.0000000 I renammed the column but still an error message what am I doing wrong? – Yannick Nov 29 '18 at 10:32
  • What is the output of `df.columns`? – Mayank Porwal Nov 29 '18 at 10:35
  • Thank you Mayank et Jezrael the function pd.to_datetime works lfine. You made me realize that I made a mistake when reading the file as I used the parameter skiprows=1 so there was no way it could find the date column… Take care guys – Yannick Nov 29 '18 at 10:42