Check if string is in pandas Dataframe column, and create new Dataframe

Viewed 9215

I am trying to check if a string is in a Pandas column. I tried doing it two ways but they both seem to check for a substring.

itemName = "eco drum ecommerce"
words = self.itemName.split(" ")
df.columns = ['key','word','umbrella', 'freq']
df = df.dropna()
df = df.loc[df['word'].isin(words)]

I also tried this way, but this also checks for substring

words = self.itemName.split(" ")
words = '|'.join(words)
df.columns = ['key','word','umbrella', 'freq']
df = df.dropna()
df = df.loc[df['word'].str.contains(words, case=False)]

The word was this: "eco drum".

Then I did this:

words = self.itemName.split(" ")
words = '|'.join(words)

To end up with this:

eco|drum

This is the "word" column:

enter image description here

Thank you, is it possible this way to not match substrings?

1 Answers
Related