【问题标题】:How to compare pandas DataFrame against None in Python?python - 如何在Python中将pandas DataFrame与None进行比较?
【发布时间】:2016-07-13 02:14:10
【问题描述】:

如何将 pandas DataFrame 与 None 进行比较?我有一个构造函数,它采用 parameter_filepandas_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 实际上包含一个熊猫数据框时):

    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

【问题讨论】:

    标签: python pandas python-2.x nonetype


    【解决方案1】:

    使用is not:

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

    PEP 8 说:

    应始终使用 isis not 与像 None 这样的单例进行比较,而不是相等运算符。

    还有一个很好的explanation为什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-06
      • 2017-07-14
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多