Looking-up part of text in a column and if found true pass a Creating a new column by reflecting assigned text value instead of true or false

Viewed 83

I want to create categories based on particular string of keyword found true than assigned category else "other".

For example - if "health" is found in a column, name that keyword row as "HEALTH", if "therapist" then 'THERAPIST'

  1. create a "category" column through code
  2. assign categories based on condition

I was able to do on Excel by creating a table and using index match and want to switch to Python to apply this on large dataset,

Below is the sample data,

keyword category
HR Consultancy UK-d-uk-159_bing other
it support COMPANY LONDON-D-UK-G1161_bing other
global sales training platform openings sales
tele private practice therapist therapist
asset grant management system other
digital team project management solution openings other
global training platform openings other
tele practice therapist therapist
global sales training platform openings sales
tele health practice health
asset grant management other
digital team project management solution other
4 Answers

The easiest solution would be

import pandas as pd
df = your dataframe

assigning the default value first. You could set to None, or np.nan too

df['category'] = 'others'
df.loc[df.keyword.str.contains('therapist'),'category'] = 'therapist'

You can use a regex with all your keywords. Then, depending on whether you want to get the first match or all, use extract or extractall with aggregation, respectively.

I added the keyword "private" as example to see a difference in row 3:

import re
words = ['health', 'therapist', 'sales', 'private']
regex = '|'.join(map(re.escape, words))
# 'health|therapist|sales|private'

# option 1: get first match
df['category_first'] = (df['keyword']
 .str.extract(f'(?i)({regex})', expand=False)
 .fillna('other')
 )

# option 2: get all matches
df['category_all'] = (df['keyword']
 .str.extractall(f'(?i)({regex})')
 [0].groupby(level=0).agg(','.join)
 .reindex(df.index, fill_value='other')
 )

print(df)

Output:

                                              keyword   category category_first       category_all
0                     HR Consultancy UK-d-uk-159_bing      other          other              other
1           it support COMPANY LONDON-D-UK-G1161_bing      other          other              other
2             global sales training platform openings      sales          sales              sales
3                     tele private practice therapist  therapist        private  private,therapist
4                       asset grant management system      other          other              other
5   digital team project management solution openings      other          other              other
6                   global training platform openings      other          other              other
7                             tele practice therapist  therapist      therapist          therapist
8             global sales training platform openings      sales          sales              sales
9                                tele health practice     health         health             health
10                             asset grant management      other          other              other
11           digital team project management solution      other          other              other
  1. Pandas has apply methods for this:
def f(s):
    if s.find('health')>=0:
        return 'health'
    if s.find('thera...')>=0:
        return 'thera'
    ...

df['category'] = df['text'].apply(f)

Read: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html

  1. pandas has map methods for this. First create a dictionary and then use map methods.
d ={keyword1:category1, ...}
df['category'] = df['keyword'].map(d)

This is for when you have a specific dictionary. Read: https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html

You can create first a list of keywords to test or research on it . Then the empty ones fill it with "other".

import pandas as pd

#example df
df=pd.DataFrame(data=['health xxxxx','yyyy therapist','zzzzz'],columns=["keyword"])

keywords=['health','therapist']


df['category']=df['keyword'].str.findall('|'.join(keywords)).apply(set).str.join(',')
df = df.apply(lambda x: x.str.strip()).replace('', "other")
Related