2016-03-25 11 views
13

मैं 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  

हालांकि, जब मैं बाद में None के खिलाफ pandas_df तुलना करने की कोशिश, (यानी जब self.pandas_df वास्तव में एक पांडा dataframe शामिल हैं):

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 

उत्तर

21

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.

भी एक अच्छा explanation क्यों नहीं है।

संबंधित मुद्दे