Pandas Loop Search Terms to Flag Rows Containing Them

Viewed 125

I have a list of search terms and a dataframe with a Description column. I'd like to flag each row that has a description containing one or more of the listed terms. I have the following started but am not sure how to correctly loop this:

terms = ['x','y']
df

for t in terms:
    df['Test_Flag'] = np.where(df['Description'].str.contains(t, case=False, na=False), t, '')

Any thoughts?

3 Answers

I suggest using apply:

import pandas
df = pd.DataFrame({'words':['joy','heX','socks','brain','zebra','sexy']})
ts = ['x','y','z']
df['flag'] = df.words.apply(lambda x: sum(t in x.lower() for t in ts) > 0)

you can build a function and use apply.

def is_in_flag(val, lst):
    for item in lst:
        if (item in val):
            return 1
    return 0

terms = ['x','y']

df['Test_Flag'] = df['Description'].apply(is_in_flag ,args=[terms])

df

enter image description here

or in your way:

terms = ['x','y']
df['Test_Flag'] = 0 
for t in terms:
    df['Test_Flag']=(( df['Test_Flag'] + df.Description.str.contains(t))>0)*1 
df

enter image description here

you cound use a regex

pattern = '|'.join(['({s})'.format(s=s) for s in terms])
df['Flag'] = df.words.str.contains(pattern)
Related