I am working on pandas dataframe (data_agg_clust) having below sample data:
KPIPred
Cluster
9-11 125.872327
18-20 120.084786
15-17 112.328802
12-14 109.752560
21-23 106.128234
I would like to create a barchart using matplotlib:
import matplotlib.pyplot as plt; plt.rcdefaults()
data_agg_clust.plot.bar(x="Cluster", y="KPIPred", rot=70, title="Predicted Volume of impressions");
plot.show(block=True);
But unfortunately I got this error:
KeyError Traceback (most recent call last)
~/.pyenv/versions/anaconda3-2021.05/envs/jupiter/lib/python3.10/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~/.pyenv/versions/anaconda3-2021.05/envs/jupiter/lib/python3.10/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
~/.pyenv/versions/anaconda3-2021.05/envs/jupiter/lib/python3.10/site-packages/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: 'Cluster'
The above exception was the direct cause of the following exception
It seems like it didn't recognize "Cluster" column. How can I fix this error?
Clusteris your DataFrame index. Try callingdata_agg_clust.reset_index()before plotting.-cswitch from the command line.