如何返回具有最高计数值“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
您可以使用^{} 。
您的代码应该如下所示:
相关问题 更多 >
编程相关推荐