Python 错误,但可以';我不明白为什么?决策树部分(关键错误)

Python 错误,但可以';我不明白为什么?决策树部分(关键错误),python,pandas,dataframe,keyerror,Python,Pandas,Dataframe,Keyerror,为什么返回键错误2, 像 KeyError回溯(最近的呼叫 最后)在 ---->1扩展树(数据2) 在扩展树中(数据) 4. 5对于范围内的值(len(数据)): ---->6如果(target2[值]=“否”): 7 n=n+1 8 elif(target2[值]=“是”): 中的~\Anaconda3\lib\site packages\pandas\core\series.py getitem(self,key) 869 key=com.apply\u如果可调用(key,self) 870

为什么返回键错误2, 像

KeyError回溯(最近的呼叫 最后)在 ---->1扩展树(数据2)

在扩展树中(数据) 4. 5对于范围内的值(len(数据)): ---->6如果(target2[值]=“否”): 7 n=n+1 8 elif(target2[值]=“是”):

中的~\Anaconda3\lib\site packages\pandas\core\series.py getitem(self,key) 869 key=com.apply\u如果可调用(key,self) 870尝试: -->871结果=self.index.get_值(self,key) 872 873如果不是标量(结果):

中的~\Anaconda3\lib\site packages\pandas\core\index\base.py 获取_值(自身、系列、键)4403 k= self.\u convert\u scalar\u indexer(k,kind=“getitem”)4404尝试: ->4405返回self.\u engine.get\u值(s,k,tz=getattr(series.dtype,“tz”,None))4406除了keyrerror 如果len(self)>0且(self.holds_integer()为e1:4407 或self.is_boolean()):

pandas中的pandas\libs\index.pyx.\u libs.index.IndexEngine.get\u value()

pandas中的pandas\libs\index.pyx.\u libs.index.IndexEngine.get\u value()

pandas中的pandas\libs\index.pyx.\u libs.index.IndexEngine.get\u loc()

pandas\u libs\hashtable\u class\u helper.pxi位于 pandas._libs.hashtable.Int64HashTable.get_item()

pandas\u libs\hashtable\u class\u helper.pxi位于 pandas._libs.hashtable.Int64HashTable.get_item()

关键错误:2


KeyError
主要是指字典中不存在键&在您的代码检查中
target2[2]
值是否存在于
expensing_树中
function很好的建议,实际上它是一个数据集,所以我删除了一些记录,在索引3和4记录也被删除的情况下,我可以重新排列数据帧的索引号吗?在删除一些行之后,您可以只知道您希望在函数“当前”或“当前”中匹配什么
def childs(data):
        target2 = data2['buys_computer']
        total_entropy2 = entropy_gain_dataset(target2)
        #total_entropy2
        
        income2 = data2['income']
        ig_income2 = total_entropy2 - (entropy_class(income2, target2))
        #ig_income2
        
        student2 = data2['student']
        ig_student2 = total_entropy2 - (entropy_class(student2, target2))
        #ig_student2
        
        rating2 = data2['credit_rating']
        ig_rating2 = total_entropy2 - (entropy_class(rating2, target2))
        #ig_rating2
        
        node = list()

        if (ig_income2 > ig_student2 and ig_income2 > ig_rating2):
            node = income2
    

        elif (ig_student2 > ig_income2 and ig_student2 > ig_rating2):
            node = student2    

        elif (ig_rating2 > ig_income2 and ig_rating2 > ig_student2):
            node = rating2
            
        return node 


def expending_tree(data):
    target2 = data2['buys_computer']
    y, n = 0, 0
    
    for value in range(len(data)):
        if (target2[value] == 'no'):
            n = n+1
        elif (target2[value] == 'yes'):
            y = y+1
    if(y == 0):
        pass
    elif (n == 0):
        pass
    else:
        x = childs(data)
        print(x)
'''