Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to move text from old column to newly created columns by using st.contains pandas

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’)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading