Victor
Victor

Reputation: 17097

Pandas isin boolean operator giving error

I am running into an error while using the 'isin' Boolean operator:

def rowcheck(row):
 return row['CUST_NAME'].isin(['John','Alan'])

My dataframe has column CUST_NAME. So I use:

df['CUSTNAME_CHK'] = df.apply (lambda row: rowcheck(row),axis=1)

I get:

'str' object has no attribute 'isin'

What did I do wrong?

Upvotes: 1

Views: 3411

Answers (1)

rafaelc
rafaelc

Reputation: 59274

You are doing it inside a function passed to apply, such that row['CUST_NAME'] holds the value for a specific cell (and it is a string). Strings which have no isin method. This method belongs to pd.Series, and not strings.

If you really want to use apply, use np.isin in this case

def rowcheck(row): 
    return pd.np.isin(row['CUST_NAME'], ['John','Alan'])

As @juanpa.arrivilaga noticed, isin won't be efficient in this case, so its advised to use the operator in directly

return row['CUST_NAME'] in ['John','Alan']

Notice that you probably don't need apply. You can just use pd.Series.isindirectly. For example,

df = pd.DataFrame({'col1': ['abc', 'dfe']})


col1
0   abc
1   dfe

Such that you can do

df.col1.isin(['abc', 'xyz'])

0     True
1    False
Name: col1, dtype: bool

Upvotes: 3

Related Questions