0

我在 Dataframe 中有一个带有命名实体值的元组,如何将每个实体的值分组到包含元组的列上并将其可视化。

s = pd.Series("At noon, Trump became the 45th president of the United States, taking the oath of office with Chief Justice John Roberts. Trump was also sworn in using two Bibles, a Bible his mother gifted him and the historic Lincoln Bible.")

print(hero.named_entities(s)[0])

哪个输出

[('noon', 'TIME', 3, 7), ('Trump', 'PERSON', 9, 14), ('45th', 'ORDINAL', 26, 30), ('the United States', 'GPE', 44, 61), ('John Roberts', 'PERSON', 108, 120), ('two', 'CARDINAL', 152, 155), ('Bibles', 'PRODUCT', 156, 162), ('Bible', 'WORK_OF_ART', 166, 171), ('Lincoln Bible', 'PERSON', 211, 224)]
4

1 回答 1

0

您没有“数据框中具有命名实体值的元组”。你有“一个元组列表”。为此,您必须遵循以下说明:

使用 for 循环遍历元组列表。在 for 循环中,使用索引语法 tuple[0] 访问每个元组的第一个元素,并以 object 作为元组的第一个元素调用 list.append(object) 将每个第一个元素附加到列表中。

如此处所示

然后,如果您愿意,可以使用 pandas 将每个列表转换为 DataFrame。

也适用于想要运行此命令的其他任何人: print(hero.named_entities(s)[0])

你必须导入以下包: import texthero as hero

于 2020-12-05T13:35:18.923 回答