0

Input df:

title                        desc
movie A                  It is a awesome movie with action
movie B                  Slow but intense movie.

I want to filter rows which contains the following keywords:

keys =  ["awesome", "action"]

Output DF:

title                        desc
movie A                  It is a awesome movie with action

Code:

index_list = []
for index,rows in df.iterrows():
   if any(x in rows["desc"].split(" ") for x in keys) == True:
       index_list.append(index)

df = df.loc[index_list]

Approach:

In each row, I am checking if any of the keywords are present after splitting the rows

This approach works fine, but I am interested to know if there is any one liner in pandas to achieve the same.

Example:

df.loc[df['column_name'].isin(some_values)]
data_person
  • 4,194
  • 7
  • 40
  • 75

2 Answers2

3

Why yes, there is - pandas.Series.str.contains

idx = df['column_name'].str.contains("|".join(keys), regex=True)
df[idx]
CJR
  • 3,916
  • 2
  • 10
  • 23
1

The following should do the trick for you:

>>> import pandas as pd
>>> d = {'title':['movie A', 'movie B'], 'desc':['It is a awesome movie with action', 'Slow but intense movie.']}
>>> df = pd.DataFrame(data=d)
>>> df
                                desc    title
0  It is a awesome movie with action  movie A
1            Slow but intense movie.  movie B
>>> keys =  ["awesome", "action"]
>>> df[df['desc'].str.contains('|'.join(keys))]
                                desc    title
0  It is a awesome movie with action  movie A
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156