[英]How to use applymap, lambda and dataframe together to filter / modify dataframe in python?
So I 'm trying to figure it out how to replace NaN
in pd.DataFrame
.. ? 所以我想弄明白如何在pd.DataFrame
替换NaN
..? Here in the example I created the 3x3 dataframe, having value in df[1][2] = 'a'
and the rest are NaN
在这个示例中,我创建了3x3数据帧,其值为df[1][2] = 'a'
,其余为NaN
My understanding is that I can use if
with in lambda
and do something below. 我的理解是我可以在lambda
使用if
并在下面做一些事情。 But the result was not what I expected, it overwrite 'a'
. 但结果不是我所期望的,它覆盖了'a'
。 I still want 'a'
to remain as it is and only change to 'o'
where is NaN
... Any recommendation would be appreciated. 我仍然希望'a'
保持原样并且只改为'o'
在哪里是NaN
...任何建议都将受到赞赏。
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
. 您可以使用fillna
而不是使用apply
。
df.fillna('o')
For more information about Working with missing data . 有关使用缺失数据的更多信息。 You could also use apply
with pd.isnull()
as mentaioned in @Psidom answer. 你也可以在@Psidom的答案中使用apply
with pd.isnull()
作为mentaioned。 But in this case, you should really use the built-in function fillna
. 但在这种情况下,你应该真正使用内置函数fillna
。
np.nan
does not really return the value you are expecting, in order to create a valid predicate, you can use pd.isnull
: np.nan
并没有真正返回你期望的值,为了创建一个有效的谓词,你可以使用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: 要了解它为什么会发生,你可以将np.nan
转换为boolean:
bool(np.nan)
# True
So it is always returning true value thus all values in the data frame will be replaced by o
. 所以它总是返回真值,因此数据帧中的所有值都将被o
替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.