Python isnull()函数

Python isnull()函数,python,pandas,Python,Pandas,我需要用1替换数据帧中的非空值,用0替换空值 这是我的数据框: my_list= [['a','b','c'],['test1','test2',None],[None,'101','000']] mydf= pd.DataFrame(my_list,columns=['col1','col2','col3']) mydf col1 col2 col3 0 a b c 1 test1 test2 None 2 None 101

我需要用1替换数据帧中的非空值,用0替换空值

这是我的数据框:

my_list= [['a','b','c'],['test1','test2',None],[None,'101','000']]

mydf= pd.DataFrame(my_list,columns=['col1','col2','col3'])

mydf

    col1   col2  col3
0      a      b     c
1  test1  test2  None
2   None    101   000

mydf.where((pd.isnull(mydf)),0,inplace=True)

mydf

   col1 col2  col3
0     0    0     0
1     0    0  None
2  None    0     0
我不知道为什么它要用零替换非空值。pd.notnull()的作用正好相反。有人能解释一下我在这里遗漏了什么吗?

只要做:

mydf = mydf.notnull() * 1
mydf

完整性

mydf.isnull() * 1

这是的预期行为。根据文档,
where
保留
True
的值并替换
False
的值,
pd.isnull
将仅对
None
条目返回
True
,这就是为什么它们是唯一被保留的条目

您可以将函数与
pd.isnull一起使用:

mydf.mask(pd.isnull(mydf), 0, inplace=True)
mydf.where(pd.notnull(mydf), 0, inplace=True)
或者您想将
where
pd.notnull
一起使用:

mydf.mask(pd.isnull(mydf), 0, inplace=True)
mydf.where(pd.notnull(mydf), 0, inplace=True)

不管怎样,@piRSquared的方法可能比上述两种方法都好

看起来不错。但是我想知道为什么pd.notnull做了相反的事情。这是一只虫子吗。这完全是误导@Rtut pd.isnull和pd.notnull是否可能存在混淆?如果值不为null,则pd.notnull返回True。@Rtut,这个答案回答了您的问题:
但我想知道pd.notnull为什么做相反的事情