In my dataframe, I have changed the NaN to 0 with the following code
df5.fillna(0, inplace=True)
However, I get the value "0.0" instead of "0". I have >150 columns in this dataframe, some need the decimals, and the ones with converted NaN values must be without decimals. How can I get that. My dataframe is for example as follows:
genome contig genes SCM SCM/genes TrfA_1__CP11611 \
source
20900_48 20900 48 1 0.0 0.00 NaN
20900_37 20900 37 130 103.0 0.79 Nan
I get:
genome contig genes SCM SCM/genes TrfA_1__CP11611 \
source
20900_48 20900 48 1 0.0 0.00 0.0
20900_37 20900 37 130 103.0 0.79 0.0
I need only "NaN" changed to "0" without affecting for example column SCM/genes. It is no option to use a code with columnnames, since I have >150 columns with NaN in this dataframe.
Thanks
I think you first filter the cols that contain NaN
, then convert these:
In [26]:
nan_cols = df.columns[df.isnull().any(axis=0)]
nan_cols
Out[26]:
Index(['TrfA_1__CP11611'], dtype='object')
In [27]:
for col in nan_cols:
df[col] = df[col].fillna(0).astype(int)
df
Out[27]:
enome contig genes SCM SCM/genes TrfA_1__CP11611
source
20900_48 20900 48 1 0.0 0.00 0
20900_37 20900 37 130 103.0 0.79 0
So this first looks for NaN
present in any rows and makes a list of the cols, you can then iterate over the cols and call fillna
and cast the dtype using astype
so that you preserve/convert the dtype.