【发布时间】:2021-04-28 06:28:03
【问题描述】:
我正在尝试查找新闻文章是否包含我已经将列表建立为数据框列的公司的特定名称。我有一个包含文章文本作为列的数据框,以及另一个包含公司名称的数据框。我想搜索每个文章文本以检测列表中是否存在任何名称,并创建包含在文本中找到的公司名称的单独变量。有人建议我使用“合并”,但由于我没有通用标识符,所以这是不可能的。我希望下面的例子能说明这个想法。
第一个数据框(文章):
| Index | Text |
|---|---|
| 1 | Apple decided to launch new product.... |
| 2 | Tesla is ... |
| 3 | IBM is paying dividend...... |
| 4 | Amazon is relocating..... |
| ...... | ........ |
带有公司名称(Compname)的第二个数据框:
| Index | Name |
|---|---|
| 1 | BP |
| 2 | Tesla |
| 3 | Bank of America |
| 4 | Amazon |
| 5 | JP Morgan |
| 6 | Apple |
| ..... | ...... |
最后我想看到的是:
| 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 |
| .... | ..... | ...... |
我尝试了类似以下的方法,但没有完全完成工作
for x in compname['Name']:
Article['Name_found']=Article['Text'].str.contains(x, na=False)
感谢您的帮助。真的很感激。
【问题讨论】:
标签: python pandas string dataframe