csv = pd.read_csv("bmi.csv")
csv.head()
I made the data frame as below by making the code as above
height weight label
0 195 64 thin
1 165 58 normal
2 130 37 normal
3 137 53 fat
4 120 40 fat
I wanted to change the height and weight values here, so I made the following code.
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]))
Since then, it has not been processed due to the following error, but is there anyone who knows?
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'
I can't because of the weight value. What is it? Masters, please tell us the answer.
python keyerror
KeyError is from csv['weight']
. If you only look at the error message, csv
means that you cannot access the key 'weight'
in the data frame, and that error occurs when there is no column named 'weight'
.
However, when I marked the data frame with .head()
, it looked like there was a column named weight
. You might want to make sure that both sides of the column name contain no blanks or printable characters.
print(csv.columns)
When you do, the column names are displayed with quotes. Check if there are any blanks.
For more clarity, see
print('weight' in csv.clumns)
Check to see if True
appears.
© 2024 OneMinuteCode. All rights reserved.