I have a dataframe that looks a bit like this:
cancelled | offer
----------|------
N | 123
N | 456
y | 789
I'm trying to use an if statement to flag whether there are any cancelled offers (i.e. ones marked "Y" or "y") in the dataframe. This is the code I have so far:
if df["cancelled"].any().isin(["Y","y"]):
print("WARNING - Cancelled offers included!")
else:
print("OK - No cancelled offers are included.")
However, when I run this I get the following error:
AttributeError: 'numpy.bool_' object has no attribute 'isin'
Obviously the isin function isn't compatible with the rest of my code, but in this case, what would be the proper method of getting the desired result?
CodePudding user response:
I think you meant df["cancelled"].isin(["Y","y"]).any()
:
if df["cancelled"].isin(["Y","y"]).any():
print("WARNING - Cancelled offers included!")
else:
print("OK - No cancelled offers are included.")
df["cancelled"].isin(["Y","y"])
returns a Series, df["cancelled"].isin(["Y","y"]).any()
converts the boolean Series into a single boolean.