pandas - 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
我请求有人指出我正确的方向。我无法知道我在哪里犯了错误
解决方案
推荐阅读
- wolfram-mathematica - 忽略 Mathematica 中具有多个变量的方程的一部分
- node.js - 如何保留简单的数据供所有用户查看
- angular - 类型“Observable<{}[]>”不可分配给类型“BrandElement[]”。'Observable<{}[]>' 类型中缺少属性 'includes'
- javascript - Javascript onclick不起作用,连续点击多个图像
- android - React Native Fetch API 返回旧数据
- sql - postgres 错误:将负数提高到非整数幂会产生复杂的结果
- spring-boot - 如何使用 Spring JPA 在 DB 中插入/更新本机查询?
- azure - 主 VM 集重新启动时故障转移如何工作?
- google-maps - Ionic 3 Google 地图 - 地图不显示在 Android 上
- python-3.x - Matplotlib Annotation Error