I have a DataFrame where some columns have NaNs values, but when I try to replace it by zero values with fillna(), It doesn't work.
print(df_sample[["Cantidad_GofrePack_lag8_rol7_med", 'Cantidad_AdultoGeneral_lag8_rol7_med']].head(5))
Cantidad_GofrePack_lag8_rol7_med Cantidad_AdultoGeneral_lag8_rol7_med
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
others_cols_with_missing = ["Cantidad_GofrePack_lag8_rol7_med", 'Cantidad_AdultoGeneral_lag8_rol7_med', (...others columns more)]
df_sample[others_cols_with_missing].fillna(value=0.0, inplace = True)
print(df_sample[["Cantidad_GofrePack_lag8_rol7_med", 'Cantidad_AdultoGeneral_lag8_rol7_med']].head(5))
Cantidad_GofrePack_lag8_rol7_med Cantidad_AdultoGeneral_lag8_rol7_med
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
I'm not getting any change, why?
CodePudding user response:
It looks like inplace=True
cannot be used for only a part of the DataFrame, like on the example, where only a few columns are gived to the fillna()
, because it creates a copy but only of this part, not for the whole DataFrame, where the NaN remain.
It works properly in this way:
df_sample[others_cols_with_missing] = df_sample[others_cols_with_missing].fillna(value=0, inplace = False, axis=1)
and now, here it is:
Cantidad_GofrePack_lag8_rol7_med Cantidad_AdultoGeneral_lag8_rol7_med
0 0.0 0.0
1 0.0 0.0
2 0.0 0.0
3 0.0 0.0
CodePudding user response:
inplace fillna
with multiple columns does not work for some reasons https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
you can do without inplace like this
dframe[['A', 'B']] = dframe[['A', 'B']].fillna(0.0)
or you can fillna all columns
dframe = dframe.fillna(0.0)