편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2019.09.26

    안녕하세요. pandas 값 변경 관련 문의입니다. KeyError


    csv = pd.read_csv("bmi.csv")
    
    csv.head()
    

    위와같은 코드를 만들어 아래와 같은 dataframe을 만들었습니다

       height       weight      label
    
    0       195     64      thin
    
    1   165         58      normal
    
    2   130         37      normal
    
    3   137         53      fat
    
    4   120     40      fat
    

    여기서 height 값과 weight값을 변경하고 싶어 다음과 같이 코드를 만들었습니다.

    csv["height"] = csv["height"]/200
    csv["weight"] = csv["weight"]/100
    print(csv["weight"])
    bclass = {"thin": [1, 0, 0], "normal" : [0, 1, 0], "fat" : [0, 0, 1]}
    csv["label_pat"] = csv["label"].apply(lambda x : np.array(bclass[x]))
    

    그 이후 아래와 같은 에러때문에 진행이 안되는데 혹시 아시는 분 계신가요.

    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: 'weight'
    
    During handling of the above exception, another exception occurred:
    
    KeyError                                  Traceback (most recent call last)
    <ipython-input-9-9d3d4ac66f19> in <module>
          1 csv["height"] = csv["height"]/200
    ----> 2 csv["weight"] = csv["weight"]/100
          3 print(csv["weight"])
          4 bclass = {"thin": [1, 0, 0], "normal" : [0, 1, 0], "fat" : [0, 0, 1]}
          5 csv["label_pat"] = csv["label"].apply(lambda x : np.array(bclass[x]))
    
    ~\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: 'weight'
    

    weight 값 때문에 안되는데 뭘까요. 고수님들 답을 알려주세요.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2019.09.26

    안녕하세요. pandas 값 변경 관련 문의입니다.


    ''' csv = pd.read_csv("bmi.csv")

    csv.head() '''

    위와같은 코드를 만들어 아래와 같은 dataframe을 만들었습니다

    height weight label

    0 195 64 thin

    1 165 58 normal

    2 130 37 normal

    3 137 53 fat

    4 120 40 fat

    여기서 height 값과 weight값을 변경하고 싶어 다음과 같이 코드를 만들었습니다.

    csv["height"] = csv["height"]/200 csv["weight"] = csv["weight"]/100 print(csv["weight"]) bclass = {"thin": [1, 0, 0], "normal" : [0, 1, 0], "fat" : [0, 0, 1]} csv["label_pat"] = csv["label"].apply(lambda x : np.array(bclass[x]))

    그 이후 아래와 같은 에러때문에 진행이 안되는데 혹시 아시는 분 계신가요

    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: 'weight'

    During handling of the above exception, another exception occurred:

    KeyError Traceback (most recent call last) in 1 csv["height"] = csv["height"]/200 ----> 2 csv["weight"] = csv["weight"]/100 3 print(csv["weight"]) 4 bclass = {"thin": [1, 0, 0], "normal" : [0, 1, 0], "fat" : [0, 0, 1]} 5 csv["label_pat"] = csv["label"].apply(lambda x : np.array(bclass[x]))

    ~\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: 'weight'

    weight 값 때문에 안되는데 ,,,뭘까요.. 고수님들 답을 알려주세요 ㅠㅠ