alex3465
alex3465

Reputation: 419

Filter dataframe with exact string match

Is there a way that str.contains filter only the exact match of string rather than partial match

List = ['good', 'bad']

df

  col
1 good
2 this is good 
3 good boy
4 bad dream
5 goodman
6 badboy
7 bad 

df[(df['col'].str.contains('|'.join(List),na=False, ))]

Desired output

1 good
6 bad 

Upvotes: 1

Views: 968

Answers (1)

Rakesh
Rakesh

Reputation: 82765

You can use isin in this senario

Ex:

data = ['good', 'bad']
print(df[df['col'].isin(data)])

Output:

    col
0  good
6   bad

Upvotes: 4

Related Questions