首页 > 解决方案 > SpaCy没有按照预期的路线填充熊猫中的数据框

问题描述

我在 spaCy 和 Python 的帮助下创建了以下数据集

import pandas as pd
import numpy as np
import spacy
from spacy.util import minibatch, compounding
from spacy.lang.en import English
df = pd.DataFrame({ 'Sentence': ["Hello Xime", "iPhone is an Apple Phone", "New Delhi is the Capital 
of India"] })
nlp = spacy.load("en_core_web_sm")
df["Col1"] = [nlp(i).ents for i  in df["Sentence"]]
df["lab"] = [[(ent.label_) for ent in ents] for ents in df["Col1"]]
print (df)

以下数据集是结果

在此处输入图像描述

我在 df 中创建了两列,即 GPE 和 ORG

df[["GPE", "ORG"]]=pd.DataFrame([[np.nan,np.nan ]], index=df.index)

接下来,如果关联的标签是 GPE 和 ORG,我尝试使用以下代码分别在 GPE 和 ORG 下的 Col1 中分配值

我也尝试了以下代码并且循环运行。

list1=["GPE", "ORG"]##CREATED A LIST
for i in list1 :df[i]= [[(ent.text) for ent in ents if ent.label_==i] for ents in df["Col1"]]

这会产生以下图像(预期结果)

在此处输入图像描述

现在我试过了

 for i in list1:
 df[i]= [[[(ent.text) for ent in ents if ent.label_==i] for ents in j ] for nlp(j) in  
 df["Sentence"]]

这会产生以下错误

  SyntaxError: can't assign to function call

我请求有人指出我正确的方向。我无法知道我在哪里犯了错误

标签: pandasdataframespacy

解决方案


推荐阅读