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?
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!")
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?
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!")