Python 使用 applymap 替换 Pandas Dataframe 中的空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34861086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:38:28  来源:igfitidea点击:

replacing null values in a Pandas Dataframe using applymap

pythonpandas

提问by useRj

I've got an "Age" column, but sometimes NaN values are displayed. I know I can use "fillna" for this purposes but I've tried to define my own function (and learning to do this way) and use applymap to dataframe

我有一个“年龄”列,但有时会显示 NaN 值。我知道我可以为此目的使用“fillna”,但我尝试定义我自己的函数(并学习这样做)并将 applymap 用于数据框

no success so far.

到目前为止没有成功。

Age
69
49
NaN
54
NaN

I've tried

我试过了

   def get_rid_of_nulls(value):
     if value == np.nan:
        return 'Is Null value'
     else:
        return value

with this not working either

这也不起作用

 if value == None
   if value isnull
   if value == np.na
   if value ==''
   if value == NaN
   if value == 'NaN'

None of the comparisons seems to work. I'm wrong for sure but I'm stuck and I'm very stubborn to use fillna

这些比较似乎都不起作用。我肯定错了,但我被卡住了,而且我很固执地使用fillna

thanks

谢谢

采纳答案by mgc

As there is "replacing" in your title, and you mentioned fillnabut not the replace()method, you can also obtain the same result doing something like that :

由于您的标题中有“替换”,并且您提到了fillna但没有提到replace()方法,因此您也可以通过执行以下操作获得相同的结果:

df.Age.replace(np.NaN, 'Is Null value', inplace=True)

# Or, depending on your needs:
df['Age'] = df.Age.replace(np.NaN, 'Is Null value')

# Or without `replace` :
df['Age'] = df.Age.apply(lambda x: x if not pd.isnull(x) else 'Is Null value')

回答by EdChum

You can use pd.isnull():

您可以使用pd.isnull()

In [4]:
def get_rid_of_nulls(value):
    if pd.isnull(value):
        return 'Is Null value'
    else:
        return value

df['Age'].apply(get_rid_of_nulls)

Out[4]:
0               69
1               49
2    Is Null value
3               54
4    Is Null value
Name: Age, dtype: object

Similarly you can use the property that NaNdoes not equal itself:

同样,您可以使用NaN不等于自身的属性:

In [5]:
def get_rid_of_nulls(value):
    if value != value:
        return 'Is Null value'
    else:
        return value

df['Age'].apply(get_rid_of_nulls)

Out[5]:
0               69
1               49
2    Is Null value
3               54
4    Is Null value
Name: Age, dtype: object