0004
0004

Reputation: 1260

Pandas - Python drop rows if Column contains multiples criteria

I am trying to delete the rows that do not contain "HW" or "CA" in column Vndr. This is my code:

data.drop(data[data.Vndr != 'HW' or 'CA'].index)

I am getting this error "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

Upvotes: 1

Views: 83

Answers (4)

Rawss24
Rawss24

Reputation: 65

Instead of deleting such rows, you can subset the rows which do not have such keywords.
You can work like this:

data = data[(data['Vndr'] != 'CA') | (data['Vndr'] != 'HW')]

Upvotes: 0

rafaelc
rafaelc

Reputation: 59274

Can use

data[data.Vndr.str.contains('HW|CA'])

General approach

s="|".join(['HW', 'CA'])
data[data.Vndr.str.contains(s)

Upvotes: 1

BENY
BENY

Reputation: 323306

You can using isin

data.loc[~data.Vndr.isin([ 'HW' , 'CA']),]

Upvotes: 1

jxramos
jxramos

Reputation: 8266

it needs to actually be

... or data.Vndr != ‘CA’

Otherwise it doesn’t make sense

Upvotes: 0

Related Questions