Fillna in multiple columns in place in Python Pandas -


i have pandas dataframe of mixed types, strings , numbers. replace nan values in string columns '.', , nan values in float columns 0.

consider small fictitious example:

df = pd.dataframe({'name':['jack','sue',pd.np.nan,'bob','alice','john'],     'a': [1, 2.1, pd.np.nan, 4.7, 5.6, 6.8],     'b': [.25, pd.np.nan, pd.np.nan, 4, 12.2, 14.4],     'city':['seattle','sf','la','oc',pd.np.nan,pd.np.nan]}) 

now, can in 3 lines:

df['name'].fillna('.',inplace=true) df['city'].fillna('.',inplace=true) df.fillna(0,inplace=true) 

since small dataframe, 3 lines ok. in real example (which cannot share here due data confidentiality reasons), have many more string columns , numeric columns. end writing many lines fillna. there concise way of doing this?

you use apply columns checking dtype whether it's numeric or not checking dtype.kind:

res = df.apply(lambda x: x.fillna(0) if x.dtype.kind in 'biufc' else x.fillna('.'))  print(res)           b     city   name 0  1.0   0.25  seattle   jack 1  2.1   0.00       sf    sue 2  0.0   0.00       la      . 3  4.7   4.00       oc    bob 4  5.6  12.20        .  alice 5  6.8  14.40        .   john 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -