Home > other >  How to change format of floar values in column with also NaN values in Pandas Data Frame in Python?
How to change format of floar values in column with also NaN values in Pandas Data Frame in Python?

Time:07-19

I have Pandas DataFrame in Python like below:

col
-------
7.0
2.0
NaN
...

"col1" is in float data type but I would like to convert displaying of floar values in this column from for example 7.0 to 7. I can not simply change date type to int because I have also "NaN" values in col1.

So as a result I need something like below:

col
-------
7
2
NaN
...

How can I do that in Python Pandas ?

CodePudding user response:

You can use convert_dtypes to perform an automatic conversion:

df = df.convert_dtypes('col')

For all columns:

df = df.convert_dtypes()

output:

    col
0     7
1     2
2  <NA>

After conversion:

df.dtypes

col    Int64
dtype: object
  • Related