TypeError:创建pd.Dataframe字典

时间:2018-05-15 13:29:06

标签: python python-3.x fuzzywuzzy

我收到的错误是:TypeError:'NoneType'对象不可订阅

在此方法中,我尝试沿两个文件(test& master)进行字符串匹配。 主文件包含正确拼写的产品名称,而测试文件包含拼写错误或这些产品的拼写版本。 我试图将它们与extractBests函数匹配,以包含某个分数的截止值。此外,如果我打印输出的早期步骤,例如fhp_new变量,它仍然有效。

我认为错误是由某种事实造成的,有些行不给出任何在score_cutoff限制内的匹配,因为当我把限制放在20时我没有得到错误。所以理论上那些行应该保持为空,但这会导致我认为的错误。

这行代码导致错误:

for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]

这是完整的代码,直到错误行

def StringMatch (master, testfile, num_match=3, score_cutoff=95, limit=3):
        master_names = master.iloc[:,3]
        test_names = testfile.iloc[:,0]    
        fhp_new = [process.extractBests(x, master_names, score_cutoff=score_cutoff,limit=limit) for x in test_names]
        lab=" "
        i=1

        while i<=num_match:
            lab = lab + " " + "Match" + str(i)
            i = i+1
        aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())

        d={}
        for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]
        print(d)

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只想查看y is None

def StringMatch (master, testfile, num_match=3, score_cutoff=95, limit=3):
        master_names = master.iloc[:,3]
        test_names = testfile.iloc[:,0]    
        fhp_new = [process.extractBests(x, master_names, score_cutoff=score_cutoff,limit=limit) for x in test_names]
        lab=" "
        i=1

        while i<=num_match:
            lab = lab + " " + "Match" + str(i)
            i = i+1
        aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())

        d={}
        for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [None if y is None else y[0] for y in aggregated_matches["Match" + str(x)]]
        print(d)