-2

How can I compare two columns of a list in python? I already tried this, but it gives the error as described below:

import pandas as pd 

TrainDate=pd.read_csv('../input/train_date.csv', delimiter=',', nrows=10, skiprows=0)
TrainDateNumeric=TrainDate.fillna(0).values

if (TrainDateNumeric[:,1] == TrainDateNumeric[:,2] ):
    print ("Yes")

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

EdChum
  • 376,765
  • 198
  • 813
  • 562
Mojtaba
  • 306
  • 3
  • 15
  • That's not a list; that's a numpy array. – Martijn Pieters Sep 09 '16 at 10:45
  • 1
    And the exception message is easily searched for. Have you tried seeing what it means? Numpy compares the contents *value by value* and produces an array of boolean results, not *one* boolean value. So there is no *one* true or false result here. – Martijn Pieters Sep 09 '16 at 10:46
  • 1
    looking at what you're trying to do here you should be doing `if (TrainDateNumeric[:,1] == TrainDateNumeric[:,2] ).all(): print ("Yes")` the issue here is that `if` expects a scalar value using `==` on dataframe columns produces a `Series` of boolean values – EdChum Sep 09 '16 at 10:52

1 Answers1

1

they are numpy array. You have to compare in this way

import numpy as np
if np.allclose(Train[:,1], Train[:,2]):
    print ("Yes")
Francesco Nazzaro
  • 2,688
  • 11
  • 20