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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…