JPC JPC - 5 months ago 11
Python Question

How to use applymap , lamda and dataframe together to filter / modify dataframe in python?

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.

enter image description here

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)

Answer

np.nan does not really return the value you are expecting, in order to create a valid predicate, you can use pd.isnull:

df = pd.DataFrame(index=range(0,3),columns=range(0,3))
df[1][2] = 'a'
f = lambda x: 'o' if pd.isnull(x) else x
df.applymap(f)

#   0   1   2
#0  o   o   o
#1  o   o   o
#2  o   a   o

To see why it is happening, you can convert np.nan to boolean:

bool(np.nan)
# True

So it is always returning true value thus all values in the data frame will be replaced by o.