Var0115
Var0115

Reputation: 21

Find columns with a specific word out of a list

I have a data frame in this format :

Id comments

23 triangles are not perfect 

43 angles are present

50 available together 

56 get them added

I want to extract columns which contain only the words 'angles' and 'get'

Expected Output:

Id comments

43 angles are present

56 get them added

Used this:

df_comments = df_comments[df_comments['comments'].str.contains("angles")]

but this returns (not getting exact matches)

Id comments

23 triangles are not perfect 

43 angles are together

Upvotes: 1

Views: 72

Answers (1)

jezrael
jezrael

Reputation: 862641

Use word boundaries by \b\b for get only exact matches with | for regex or:

L = ['angles','get']
pat = '|'.join(r"\b{}\b".format(x) for x in L)
df_comments = df_comments[df_comments['comments'].str.contains(pat)]
print (df_comments)
   Id            comments
1  43  angles are present
3  56      get them added

Upvotes: 2

Related Questions