I want to move around description text column to newly created columns based on keywords in python.
For example, if keywords are ‘Table’, ‘Fan’, ‘Chair’
Description(Given) Keyword Table Keyword Fan Keyword Chair
The table is long The table is long
The fan is nice The fan is nice
The fan is cheap The fan is cheap
The chair is brown The chair is brown
I tried to use both str.contains() and str.findall(), but it gives either T|F boolean or just the keyword (ex. ‘chair’)
df['Keyword Table'] = df['Description'].str.contains('Table')
AND
keywords=['Table']
df['Keyword Table'] = df['Description'].str.findall((keywords)).apply(set)
>Solution :
Your boolean series can be used as index to slice your dataframe, like this:
df['Keyword Table'] = df[df['Description'].str.contains('Table', na = False)]['Description']
For a list of keywords, you can use apply:
keywords = ['Table', 'Fan', 'Chair']
df['Keywords'] = df[df['Description'].apply(lambda x: any(k in x for k in keywords))]['Description']