Vahid the Great
Vahid the Great

Reputation: 483

Using split function in Pandas bracket indexer

I'm trying to keep the text rows in a data frame that contains a specific word. I have tried the following:

df['hello' in df['text_column'].split()]

and received the following error:

'Series' object has no attribute 'split'

Please pay attention that I'm trying to check if they contain a word, not a char series, so df[df['text_column'].str.contains('hello')] is not a solution because in that case, 'helloss' or 'sshello' would also be returned as True.

Upvotes: 0

Views: 66

Answers (1)

Daniel Wyatt
Daniel Wyatt

Reputation: 1151

Another answer in addition to the regex answer mentioned above would be to use split combined with the map function like below

df['keep_row'] = df['text_column'].map(lambda x: 'hello' in x.split())
df = df[df['keep_row']]

OR

df = df[df['text_column'].map(lambda x: 'hello' in x.split())]

Upvotes: 2

Related Questions