I have the following code:
for i in catvarList:
cat_num_0 = df_cat_0[i].value_counts(normalize=True)
f,(ax1,ax2,ax3) = plt.subplots(1,3,sharey=True,gridspec_kw={'width_ratios':[1,1, 0.001]},figsize=(15,4))
print("Graph for : ",i)
chart1=sns.barplot(x=cat_num_0.index, y=cat_num_0, ax=ax1,palette="Blues_d")
chart1.set_xticklabels(chart1.get_xticklabels(), horizontalalignment='right')
chart1.axes.set_title('Target 0',fontsize= 12,fontweight= 7,color='Green')
cat_num_1=df_cat_1[i].value_counts(normalize=True)
chart2=sns.barplot(x=cat_num_1.index, y=cat_num_1,ax=ax2,palette="Blues_d")
chart2.set_xticklabels(chart2.get_xticklabels(), horizontalalignment='right')
chart2.axes.set_title('Target 1',fontsize= 12,fontweight= 7,color='Green')
plt.show()
But I am constantly getting this error:
KeyError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3621, in Index.get_loc(self, key, method, tolerance)
3620 try:
-> 3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
File ~\anaconda3\lib\site-packages\pandas\_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()
File ~\anaconda3\lib\site-packages\pandas\_libs\index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()
File pandas\_libs\hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item()
File pandas\_libs\hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'OWN_CAR_flag'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Input In [72], in <cell line: 1>()
1 for i in catvarList:
----> 2 cat_num_0 = df_cat_0[i].value_counts(normalize=True)
3 f,(ax1,ax2,ax3) = plt.subplots(1,3,sharey=True,gridspec_kw={'width_ratios':[1,1, 0.001]},figsize=(15,4))
4 print("Graph for : ",i)
File ~\anaconda3\lib\site-packages\pandas\core\frame.py:3505, in DataFrame.__getitem__(self, key)
3503 if self.columns.nlevels > 1:
3504 return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
3506 if is_integer(indexer):
3507 indexer = [indexer]
File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3623, in Index.get_loc(self, key, method, tolerance)
3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
-> 3623 raise KeyError(key) from err
3624 except TypeError:
3625 # If we have a listlike key, _check_indexing_error will raise
3626 # InvalidIndexError. Otherwise we fall through and re-raise
3627 # the TypeError.
3628 self._check_indexing_error(key)
KeyError: 'OWN_CAR_flag'
I have been using this project as a reference. It is the same but i am stuck on line 58 of the code provided here, and I could not find a solution by searching.
What am I doing wrong, and how can I fix the error?
df_cat_0
? A pandas dataframe? If so, what are its columns? It seems like you are trying to get column'OWN_CAR_flag'
from the dataframe, but there is no such column.KeyError
. You should make sure you have an understanding of what error messages mean and how they work, before trying to follow a specific project tutorial or learning to use third-party libraries like Pandas. If you do understand, then please try to use that understanding to look for a cause first. For example, when the error happens, what is the value ofi
? In that situation, why shoulddf_cat_0[i]
work?'OWN_CAR_flag'
is created in cell[32]
. Make sure you created it before runing the loop