我使用以下代码来标识列类型是否为“GPE”,这意味着字段包含地缘政治实体的名称
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'])
df['new_col'] = df['text2'].apply(lambda x: [[w.label_] for w in list(nlp(x).ents)])
df
但是,它一次只能应用于一列,我想知道如何修改它,以便它可以应用于数据帧中的所有列,然后只返回包含多个“GPE”的列的名称作为数据类型
所需输出(显示列“text”,因为两行都是GPE):
text
您可以使用数据集列,对它们进行迭代,并使用相同的逻辑将新列追加到现有数据集,如下所示:
从这个获得的更新数据帧中,可以应用过滤器删除不包含GPE值的列
相关问题 更多 >
编程相关推荐