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.