0

I am a student and i am learning pandas.

I have created excel file named Student_Record.xlsx(using microsoft excel) in this sheet, we have DOB and YOP columns, which does not have time stamp

I wanted to create new file using pandas

import pandas as pd
df = pd.read_excel(r"C:\Users\sudarshan\Desktop\Student_Record.xlsx")
df.head()
df.to_excel(r"C:\Users\sudarshan\Desktop\Output.xlsx",index=False)

I opened the file in pandas and saving the file back to excel with different name(file name = Output)

I saved the file back to Excel, but when i open the file(Output) on MS.Excel the columns(DOB and YOP)have time stamp attached to dates.

 this Out put file added Time stamp infront of Date

Please let me know how to print only date?(I want Output file and its contents to look exactly like the original file)

Hope to get some help/support. Thank you

  • Please [do not post images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) of your data. You can include [code that creates a dataframe or the output of `print(df)`](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) (or of a few rows and columns that allow to reproduce the example) – Cimbali Jun 19 '21 at 16:24

1 Answers1

0

Probably your DOB and Year of passing columns are of datetime format before they are saved to Excel. As a result, they got converted back to the datetime representation when saved to Excel.

If you want to retain its contents to look exactly like the original file in dd-mm-YYYY format, you can try converting these 2 columns to string format before saving to Excel. You can do it by:

df['DOB'] = df['DOB'].dt.strftime('%d-%m-%Y')
df['Year of passing'] = df['Year of passing'].dt.strftime('%d-%m-%Y')
SeaBean
  • 22,547
  • 3
  • 13
  • 25