One hot encoding後 使用head 顯示錯誤
2019/04/25 下午 04:28
機器學習共學討論版
林庭延
觀看數:2
回答數:1
收藏數:0
ml100-2
head
one hot encoding
ml100-2-d04
錯誤代碼
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2656 try:
-> 2657 return self._engine.get_loc(key)
2658 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
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: 'OCCUPATION_TYPE'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-108-7f1da93d18f8> in <module>
1 app_train = pd.get_dummies(app_train)
2 print(app_train['CODE_GENDER_M'].head())
----> 3 print(app_train['OCCUPATION_TYPE'].head())
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2925 if self.columns.nlevels > 1:
2926 return self._getitem_multilevel(key)
-> 2927 indexer = self.columns.get_loc(key)
2928 if is_integer(indexer):
2929 indexer = [indexer]
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2657 return self._engine.get_loc(key)
2658 except KeyError:
-> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
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: 'OCCUPATION_TYPE'
import os
import numpy as np
import pandas as pd
# 設定 data_path, 並讀取 app_train dir_data = './data/' f_app_train = os.path.join(dir_data, 'application_train.csv') app_train = pd.read_csv(f_app_train)
app_train = pd.get_dummies(app_train)
print(app_train['CODE_GENDER_M'].head())
print(app_train['OCCUPATION_TYPE'].head())
CODE_GENDER_M 能正常顯示
為什麼OCCUPATION_TYPE 不能呢?
回答列表
-
2019/04/25 下午 05:27張維元 (WeiYuan)贊同數:2不贊同數:0留言數:3
這個錯誤訊息:KeyError:'OCCUPATION_TYPE',表示你的 DataFrame 中找不到這個欄位。建議你先把 app_train 印出來看看