So I 'm trying to figure it out how to replace NaN
in pd.DataFrame
.. ?
Here in the example I created the 3x3 dataframe, having value in df[1][2] = 'a'
and the rest are NaN
My understanding is that I can use if
with in lambda
and do something below. But the result was not what I expected, it overwrite 'a'
. I still want 'a'
to remain as it is and only change to 'o'
where is NaN
... Any recommendation would be appreciated.
df = pd.DataFrame(index=range(0,3),columns=range(0,3))
df[1][2] = 'a'
f = lambda x: 'o' if np.nan else x
df.applymap(f)
Instead of using apply
, you could use fillna
.
df.fillna('o')
For more information about Working with missing data. You could also use apply
with pd.isnull()
as mentaioned in @Psidom answer. But in this case, you should really use the built-in function fillna
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With