해결된 질문
작성
·
200
0
코랩으로 입력중인데
from sklearn.preprocessing import LabelEncoder cols = ['Gender','Education_Level','Marital_Status','Income_Category','Card_Category'] for col in cols: le=LabelEncoder() train[col] = le.fit_transform(train[col]) test[col] = le.transform(test[col]) target = train.pop('Attrition_Flag')
에러가 나와요 왜그런거죠
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3801 try:
-> 3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
6 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Attrition_Flag'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, checkindexing_error will raise
KeyError: 'Attrition_Flag'
답변 1
0
KeyError: 'Attrition_Flag'는 'Attrition_Flag'컬럼이 없을 때 발생합니다.
아마 이전코드에서 train에서 이미 'Attrition_Flag'컬럼을 제거한 것이 아닌가 싶어요
코드를 다시 확인해주세요