How to check if the Last Column is different from the Second last Column? Unresolved attribute reference 'any' for class 'bool

Question:

I have a Pandas Series with dtype: Boolean

If the second last and last column are different from each other then do the part in the If-Condition.

I have two different solutions, but with both I get an error:

Unresolved attribute reference ‘any’ for class ‘bool’

Solution 1:

difference = result[result.columns[-1]] != result[result.columns[-2]]
    if difference.any():
        ...
    else:
        print("Error!")

Solution 2:

difference = result[result.columns[-1]] != result[result.columns[-2]]
    if True in difference.values:
        ...
    else:
        print("Error!")

Is there a third Solution where I dont get error?

Asked By: SebastianHeeg

||

Answers:

If need compare Series there are no column names, ouput is bool True or False:

difference = result[-1] != result[-2]
    if difference:
        ...
    else:
        print("Error!")

If need compare columns in result DataFrame and Series.any failed you can use numpy.any:

difference = result.iloc[:, -1] != result.iloc[:, -2]
if np.any(difference):
    ...
else:
    print("Error!")
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.