0

I'm trying to insert a new column in Python using Pandas based on an "or" condition, but I'm having trouble with the code. Here's what I'm trying to do:

If the column "Regulatory Body" says FDIC, Fed, or Treasury, then I want a new column "Filing" to say "Yes"; otherwise, say "No". This is what I've written. My dataframe is df200.

    df200["Filing"] = np.where(df200["Regulatory Body"]=="FDIC","Yes","No")

Is there a way to have an "or" condition in this code to fit the other two variables?

Thanks!

1 Answer 1

3

Yes. Use pd.Series.isin:

bodies = {'FDIC', 'Fed', 'Treasury'}

df200['Filing'] = np.where(df200['Regulatory Body'].isin(bodies), 'Yes', 'No')

Alternatively, use pd.Series.map with the Boolean array you receive from pd.Series.isin:

df200['Filing'] = df200['Regulatory Body'].isin(bodies).map({True: 'Yes', False: 'No'})
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.