如何在 Python 中将 Pandas DataFrame 与 None 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36217969/
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
How to compare pandas DataFrame against None in Python?
提问by CiaranWelsh
How do I compare a pandas DataFrame with None
? I have a constructor that takes one of a parameter_file
or a pandas_df
but never both.
如何将 Pandas DataFrame 与None
? 我有一个构造函数,它采用 aparameter_file
或 a之一,pandas_df
但从不采用两者。
def __init__(self,copasi_file,row_to_insert=0,parameter_file=None,pandas_df=None):
self.copasi_file=copasi_file
self.parameter_file=parameter_file
self.pandas_df=pandas_df
However, when I later try to compare the pandas_df
against None
, (i.e. when self.pandas_df
actually contains a pandas dataframe):
但是,当我稍后尝试比较pandas_df
与None
, 时(即self.pandas_df
实际包含Pandas数据框时):
if self.pandas_df!=None:
print 'Do stuff'
I get the following TypeError:
我收到以下类型错误:
File "C:\Anaconda1\lib\site-packages\pandas\core\internals.py", line 885, in eval
% repr(other))
TypeError: Could not compare [None] with block values
回答by Mike Müller
Use is not
:
使用is not
:
if self.pandas_df is not None:
print 'Do stuff'
PEP 8says:
PEP 8说:
Comparisons to singletons like
None
should always be done withis
oris not
, never the equality operators.
与单例的比较
None
应该总是用is
or来完成is not
,而不是等号运算符。
There is also a nice explanationwhy.
还有一个很好的解释为什么。