J Ng
J Ng

Reputation: 799

How to specify multiple elements using pandas.Series.isin

I have a working code that I am trying to reduce

df['Criteria'] = (df['Alpha'] == 3) | (df['Alpha'] == 4) 

I tried with error the below (TypeError: isin() takes 2 positional arguments but 3 were given)

df['Criteria'] = df['Alpha'].isin(3,4)

I took reference from Pythonic Way to have multiple Or's when conditioning in a dataframe

Can anyone advise me on this please? Thank you

Upvotes: 0

Views: 1392

Answers (1)

jezrael
jezrael

Reputation: 863611

You are close, need list,tuple, array or set in isin:

Series.isin(values)

values : set or list-like

The sequence of values to test. Passing in a single string will raise a TypeError. Instead, turn a single string into a list of one element.

df['Criteria2'] = df['Alpha'].isin([3,4])

Upvotes: 2

Related Questions