1

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?

2
  • 1
    It seems like Cluster is your DataFrame index. Try calling data_agg_clust.reset_index() before plotting. Commented Jan 20, 2022 at 10:13
  • Please don't use semicolons in your python code if you can avoid it. The only reason they exist is so you can use the -c switch from the command line. Commented Jan 20, 2022 at 10:30

1 Answer 1

1

If your dataframe's index has a name, pandas will distinguish it visually by printing it on a line below the column names. In this case Cluster is the name of your index.

The simplest solution is to leave x alone: it will default to the index if you don't supply a column name:

data_agg_clust.plot.bar(y="KPIPred", rot=70, title="Predicted Volume of impressions")

Another solution is to call data_agg_clust.reset_index to convert the index into a column that can be used for plotting:

data_agg_clust.reset_index().plot.bar(x="Cluster", y="KPIPred", rot=70, title="Predicted Volume of impressions")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.