How to preserve the data types when writing pandas DataFrame to Excel?

I’m having an issue with how pandas could not preserve the data types of my table after writing process.

This is my table in which I read the data from SQL Server with my query.

And I simply code this line to write a new one hoping that it will yie…


This content originally appeared on DEV Community and was authored by Retiago Drago

I'm having an issue with how pandas could not preserve the data types of my table after writing process.

This is my table in which I read the data from SQL Server with my query.

Initial

And I simply code this line to write a new one hoping that it will yield the same result when I read it later.

df.to_excel('new2.xlsx', index=False, engine='xlsxwriter')
df2 = pd.read_excel('new2.xlsx')

And I ended up with this.

After writing process

I'm having a hard time finding the right keywords and solutions on Stack Overflow. I've already read the pandas documentation but nothing covers my case.


This content originally appeared on DEV Community and was authored by Retiago Drago


Print Share Comment Cite Upload Translate Updates
APA

Retiago Drago | Sciencx (2021-11-14T12:43:43+00:00) How to preserve the data types when writing pandas DataFrame to Excel?. Retrieved from https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/

MLA
" » How to preserve the data types when writing pandas DataFrame to Excel?." Retiago Drago | Sciencx - Sunday November 14, 2021, https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/
HARVARD
Retiago Drago | Sciencx Sunday November 14, 2021 » How to preserve the data types when writing pandas DataFrame to Excel?., viewed ,<https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/>
VANCOUVER
Retiago Drago | Sciencx - » How to preserve the data types when writing pandas DataFrame to Excel?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/
CHICAGO
" » How to preserve the data types when writing pandas DataFrame to Excel?." Retiago Drago | Sciencx - Accessed . https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/
IEEE
" » How to preserve the data types when writing pandas DataFrame to Excel?." Retiago Drago | Sciencx [Online]. Available: https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/. [Accessed: ]
rf:citation
» How to preserve the data types when writing pandas DataFrame to Excel? | Retiago Drago | Sciencx | https://www.scien.cx/2021/11/14/how-to-preserve-the-data-types-when-writing-pandas-dataframe-to-excel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.