1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.