提升DataFrame中列表的比较速度

2024-10-03 13:27:41 发布

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

我正在尝试将pd.DataFrame(sports.pubdf)的单元格中的标记列表与另一个单元格的列表进行比较,并在另一列的列表中生成命中和非命中。 为此,我使用了嵌套循环,如

for i in sports.pubdf.index:
hitlist=[]
falselist=[]
print("Control: {0} of {1}".format(sports.pubdf.index.get_loc(i), len(sports.pubdf.index)))
for meshterm in sports.pubdf['Supplemented MeSH-Terms'][i]:
    if meshterm in sports.pubdf['auto-MeSH-Terms'][i]:
        hitlist.append(meshterm)
        sports.pubdf.set_value(i,'Hits',hitlist)
    elif meshterm not in sports.pubdf['auto-MeSH-Terms'][i]:
        falselist.append(meshterm)
        sports.pubdf.set_value(i, 'Non-Hits',falselist)

这是可行的,但速度非常慢(大约3600行的长度需要几分钟)。我知道np.where应该有可能,但我的版本不起作用:

sports.pubdf['Hits'] = np.where(pubdf['auto-MeSH-Terms'].isin(sports.pubdf['Supplemented MeSH-Terms'])

它说“对象没有属性‘isin’?”

有什么建议吗


Tags: in列表forautoindexmeshtermssports