在熊猫数据框中查找无序列表的更快方法

时间:2019-04-09 15:42:37

标签: python pandas numpy nested

我正在尝试检查我的熊猫数据框行中是否存在新的样本列表。

问题是我必须遍历所有测试样本,将每个无序列表的测试样本与预定义的“有效”数据帧的所有行进行比较。

我已经使用2个嵌套的for循环解决了该问题,并设置了比较来解决该问题,但是每个样本的查找时间几乎都是一秒钟。例如。如果我必须测试1000个样本,则需要1000秒。

此功能比较两个列表中的每个元素。

from collections import Counter

def compare(s, t):
    if Counter(s) == Counter(t):
        return 1
    else:
        return 0

This look_up function iterate through my valid set and calls compare function to find if a match in the valid set.

def look_up(test):
    freq=0
    for i in range(len(valid_sets)):
        a=valid_sets.iloc[i,0]
        if compare(a,test)==1:
            freq=valid_sets.iloc[i,1]
    if freq>0:
        return "exists"

    else:
        return "missing"

This part loops through samples my test dataframe.
pred_test=[]
for i in range(len(proc_test_df)):
    print("sample point",i)
    sample=proc_test_df.iloc[i,:]
    pred_test.append(look_up(sample))

任何人都可以通过广播或类似方法帮助我获得矢量化结果或更有效的答案。 预先感谢

0 个答案:

没有答案