Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

python - How to create a new columns of dataframe based on string containing condition

I have a pandas dataframe like follows:

df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 
                    'Phrases':['I have a cool family', 'I like avocados', 'I would like to go to school', 'I enjoy Harry Potter']}) 

and a list of keywords as follows

l=['cool','avocado','lord of the rings']

I want to create a new column in the dataframe with True/False values. It will depend on whether or not each entity in "Phrases" contains one or more keywords for the list "l". In this case, the new column should read True, True, False, False.

This is simple for short dataframes, with

for i in ...
    str(bool([ele for ele in (keyword list) if(ele in df.Phrases[i])] ))

but a for loop is not reasonable for data frames of >1000000 rows, like my real one. Is there a more efficient way to create a new column with these True/False values.

question from:https://stackoverflow.com/questions/65948727/how-to-create-a-new-columns-of-dataframe-based-on-string-containing-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it with pd.Series.str.contains with giving the list l as a OR string :

import re
import pandas as pd

df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 
                    'Phrases':['I have a cool family', 'I like avocados', 'I would like to go to school', 'I enjoy Harry Potter']})

l=['cool','avocado','lord of the rings']

df['new_column']=df['Phrases'].str.contains('|'.join(l))

df['matched strings']=df['Phrases'].apply(lambda x: ','.join(re.findall('|'.join(l),x)))


df
Out[18]: 
        Date                       Phrases  new_column matched strings
0  10/2/2011          I have a cool family        True            cool
1  11/2/2011               I like avocados        True         avocado
2  12/2/2011  I would like to go to school       False                
3  13/2/2011          I enjoy Harry Potter       False                

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...