Reputation: 161
Pandas dataframe to_csv() performs unwanted format conversion by default.
I have an Excel sheet that contains columns with integer values that refer to option items in a web application. I go to considerable lengths to convert these integer values to strings before exporting them to CSV. However, when I do, the to_csv() still converts a "1" to "1.0" and "0" to "0.0". First question is, how do I prevent Pandas from doing that? And second question, why does to_excel() NOT show this behavior. If I export my dataframe to Excel the integer values are correctly preserved.
I'm using Pandas 0.23.4 in a virtual environment on Mac OSX Mojave.
Any help is appreciated!
Upvotes: 1
Views: 1649
Reputation: 394269
If you find that your types have changed to a float when originally you thought that it was integer or all strings, then it's most likely that you have missing values such as NaN
, None
.
So you can either drop those rows using dropna
:
df[column] = df[column].dropna()
or replace/fill these values:
df[column] = df[column].fillna(some_val)
where some_val
is 0
, or whatever value you want.
This will then ensure the dtype for that column is not mixed or upcasted to float
.
Upvotes: 1