首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向现有数据框添加SpaCy输出时,列不对齐

问向现有数据框添加SpaCy输出时,列不对齐
EN

Stack Overflow用户
提问于 2021-03-24 19:47:03
回答 2查看 75关注 0票数 0

我有一个包含一列文章标题的csv,我使用SpaCy从其中提取出现在标题中的任何人的姓名。当尝试使用由SpaCy提取的名称向csv添加新列时,它们不会与从中提取它们的行对齐。

我相信这是因为SpaCy结果有自己的索引,独立于原始数据的索引。

我尝试将索引添加到新的列行中,但得到"ValueError:传递的值的长度是2,索引意味着10“。

如何将SpaCy输出与它们所在的行对齐?

下面是我的代码:

代码语言:javascript
复制
import pandas as pd
from pandas import DataFrame
df = (pd.read_csv(r"C:\Users\Admin\Downloads\itsnicethat (5).csv", nrows=10,
                  usecols=['article_title']))
article = [_ for _ in df['article_title']]

import spacy
nlp = spacy.load('en_core_web_lg')
doc = nlp(str(article))
ents = list(doc.ents)
people = []
for ent in ents:
    if ent.label_ == "PERSON":
        people.append(ent)

import numpy as np
df['artist_names'] = pd.Series(people)
print(df.head())

这是生成的数据帧:

代码语言:javascript
复制
                                       article_title       artist_names
0  “They’re like, is that? Oh it’s!” – ...               (Hannah, Ward)
1  Billed as London’s biggest public festival of ...  (Dylan, Mulvaney)
2  Transport yourself back to the dusky skies and...                NaN
3  Turning to art at the beginning of quarantine ...                NaN
4  Dylan Mulvaney, head of design at Gretel, expl...                NaN

这就是我所期待的:

代码语言:javascript
复制
                                       article_title       artist_names
0  “They’re like, is that? Oh it’s!” – ...               (Hannah, Ward)
1  Billed as London’s biggest public festival of ...                NaN
2  Transport yourself back to the dusky skies and...                NaN
3  Turning to art at the beginning of quarantine ...                NaN
4  Dylan Mulvaney, head of design at Gretel, expl...   (Dylan, Mulvaney)

您可以看到artist_names列中的第5个值与第5个文章标题相关。我如何才能使它们对齐?

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-24 20:21:30

我将遍历文章,分别检测每个文章中的实体,并将检测到的实体放在一个列表中,每个文章一个元素:

代码语言:javascript
复制
nlp = spacy.load('en_core_web_lg')
article = [_ for _ in df['article_title']]

entities_by_article = []
for doc in nlp.pipe(article):
  people = []
  for ent in doc.ents:
    if ent.label_ == "PERSON":
      people.append(ent)
  entities_by_article.append(people)

df['artist_names'] = pd.Series(entities_by_article)

注意:for doc in nlp.pipe(article)是spaCy更高效的遍历文本列表的方法,可以替换为:

代码语言:javascript
复制
for a in article:
  doc = nlp(a)
  ## rest of code within loop
票数 1
EN

Stack Overflow用户

发布于 2021-03-24 19:57:15

代码语言:javascript
复制
    if ent.label_ == "PERSON":
        people.append(ent)
    else:
        people.append(np.nan) # if ent.label_ is not a PERSON

包括else语句,因此如果PERSON不是PERSON,则将其视为NaN。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66780391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档