How to detect a string in dataframe column from a list of names in another dataframe column

Viewed 48

I am trying to find whether a news article contains a specific name of the company which I already have established a list as a dataframe column. I have one dataframe that contains the text of article as a column, and another dataframe with the names of the companies. I would like to search each article text to detect whether any name from the list exists, and create separate variable containing that name of the company found within the text. Someone recommended me using 'merge', but since I do not have the common identifier, it was not possible. I hope following example illustrates the idea.

First Dataframe (Article):

Index Text
1 Apple decided to launch new product....
2 Tesla is ...
3 IBM is paying dividend......
4 Amazon is relocating.....
...... ........

Second Dataframe with company name (Compname):

Index Name
1 BP
2 Tesla
3 Bank of America
4 Amazon
5 JP Morgan
6 Apple
..... ......

What I want to see in the end would be the following:

Index Text Name_found
1 Apple decided to launch new product.... Apple
2 Tesla is ... Tesla
3 IBM is paying dividend...... NaN
4 Amazon is relocating..... Amazon
.... ..... ......

I tried something like the following, but didn't quite get the job done

for x in compname['Name']:
    Article['Name_found']=Article['Text'].str.contains(x, na=False)

Thank you for your help. Truly appreciate it.

1 Answers

Do you want this - >

pattern = r'(' + '|'.join(df1['Name'].to_list()) + ')'
df2['Text'] = df2['Text'].str.extract(pat= pattern)
print(df2)

Idea is to make a regex pattern with multiple or conditions - Here, for this case pattern will look like this -

'(BP|Tesla|Bank of America|Amazon|JP Morgan|Apple)'

Output- >

  Index    Text
0      1   Apple
1      2   Tesla
2      3     NaN
3      4  Amazon
Related