Python pandas dataframe convert values without decimals -


in dataframe, have changed nan 0 following code

df5.fillna(0, inplace=true) 

however, value "0.0" instead of "0". have >150 columns in dataframe, need decimals, , ones converted nan values must without decimals. how can that. dataframe example 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 "nan" changed "0" without affecting example column scm/genes. no option use code columnnames, since have >150 columns nan in dataframe.

thanks

i think first filter cols contain nan, 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]: 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 first looks nan present in rows , makes list of cols, can iterate on cols , call fillna , cast dtype using astype preserve/convert dtype.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -