python - Pandas fillna: Output still has NaN values -


i having strange problem in pandas. have dataframe several nan values. thought fill nan values using column means (that is, fill every nan value column mean) when try following

  col_means = mydf.apply(np.mean, 0)   mydf = mydf.fillna(value=col_means) 

i still see nan values. why?

is because have more nan values in original dataframe entries in col_means? , difference between fill-by-column vs fill-by-row?

you can fillna df.mean() series (which dict-like):

in [11]: df = pd.dataframe([[1, np.nan], [np.nan, 4], [5, 6]])  in [12]: df out[12]:     0   1 0   1 nan 1 nan   4 2   5   6  in [13]: df.fillna(df.mean()) out[13]:    0  1 0  1  5 1  3  4 2  5  6 

note: df.mean() row-wise mean, gives fill values:

in [14]: df.mean() out[14]: 0    3 1    5 dtype: float64 

note: if df.mean() has nan values these used in dataframe's fillna, perhaps want use fillna on series i.e.

df.mean().fillna(0) df.fillna(df.mean().fillna(0)) 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -