Search code examples
pythonpandasfloating-pointscientific-notationnumber-formatting

Pandas format - How to save a DataFrame float64 column (with NaNs) as int?


My DataFrame has about 20 columns, with mixed column types; one of them is a 15 to 18 digits ID number. Some rows don't have an ID number (there are NaNs in the column). When reading the .csv, the ID number is written using scientific notation, losing the benefit of an ID number...

I am trying to find a way to save the DataFrame as a csv (using .to_csv), while keeping this ID number in full int form.

The closest thing I found was Format / Suppress Scientific Notation from Python Pandas Aggregation Results, but it changes all the columns, where I would like to change only the one.

Thanks for your help!


Solution

  • As MaxU said in the comments, the best way is likely to use a placeholder for the NaNs.

    I used .fillna(-9999) on my column to remove the NaNs,then it's easy to express the ID as int (using .astype(int) or dtype).

    Problem solved.