如何选择某个值的最高计数列

2024-10-05 13:18:57 发布

您现在位置:Python中文网/ 问答频道 /正文

如何返回具有最高计数值“GPE”的列名?在这种情况下,我希望我的输出只是“text”,因为该列有两行“GPE”,而列text2有1,列text3有0

代码:

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 # obtains the columns of the dataframe

for col in col_list:
    df["".join(col)] = df[col].apply(lambda x: [[w.label_] for w in list(nlp(x).ents)]) # combine the ent_<<col_name>> as the new columns which contain the named entities.
df

期望输出:

text

Tags: columnsthetextcoreimportwebdfas
1条回答
网友
1楼 · 发布于 2024-10-05 13:18:57

您可以使用^{}。 您的代码应该如下所示:

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 # obtains the columns of the dataframe

maxGPE = 0
index = ''
for col in col_list:
    newGPE = df[col].apply(lambda x: x in nlp(x).ents).sum()
    if newGPE > maxGPE: 
        index = col 
        maxGPE = newGPE
print(index)

相关问题 更多 >

    热门问题