user2744315
user2744315

Reputation: 77

Change Values of Column in Python based on multiple conditions

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!

Upvotes: 0

Views: 68

Answers (1)

jpp
jpp

Reputation: 164623

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'})

Upvotes: 3

Related Questions