【发布时间】:2020-06-30 20:47:37
【问题描述】:
我有以下代码可以正常工作:
import spacy
import pandas as pd
import en_core_web_sm
nlp = en_core_web_sm.load()
text = [["Canada", 'University of California has great research', "non-location"],["China", 'MIT is at Boston', "non-location"]]
df = pd.DataFrame(text, columns = ['text', 'text2', 'text3'])
col_list = df.columns
for col in col_list:
df[col] = df[col].apply(lambda x: [[w.label_] for w in list(nlp(x).ents)])
df
但是,当我做同样的事情但有一个额外的数字列时,我收到错误:“Argument 'string' has wrong type (expected str, got list)”。
text = [["Canada", 'University of California has great research', "non-location", 2],["China", 'MIT is at Boston', "non-location", 3]]
df = pd.DataFrame(text, columns = ['text', 'text2', 'text3', 'text4'])
col_list = df.columns
for col in col_list:
df[col] = df[col].apply(lambda x: [[w.label_] for w in list(nlp(x).ents)])
我的问题是如何使它与整数一起工作或简单地忽略整数类型的数据列?
【问题讨论】:
-
你能在不使用 pandas 的情况下简化这段代码来生成问题吗? (如果没有,添加
pandas标签而不是单独使用python标签)。