如何在Python中将pandas数据帧与None进行比较?

2024-05-13 09:08:24 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将pandas数据帧与None进行比较?我有一个构造函数,它接受一个parameter_file或一个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      

但是,当我稍后尝试将pandas_dfNone进行比较时(即self.pandas_df实际上包含pandas数据帧时):

    if self.pandas_df!=None:
        print 'Do stuff'

我得到以下类型错误:

  File "C:\Anaconda1\lib\site-packages\pandas\core\internals.py", line 885, in eval
    % repr(other))

TypeError: Could not compare [None] with block values

Tags: to数据selfnonepandasdfifparameter
1条回答
网友
1楼 · 发布于 2024-05-13 09:08:24

使用is not

if self.pandas_df is not None:
    print 'Do stuff'

PEP 8说:

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

还有一个很好的原因。

相关问题 更多 >