0

I have been facing one issue while I am trying to plot a bar graph using the matplotlib library.

Please find the sample data below

Sample Data Image

count_movies_year = n_db.groupby('release_year').agg({'title':'count'}).rename(columns={'title':'no_of_titles'})
count_movies_year.reset_index()

I have written the above code and did the group_by on certain cases and renamed the column in the dataframe that I have in place. Now after this I wanted to plot a bar graph of the same using the matplotlib and I have written the below code

plt.bar(count_movies_year['release_year'],count_movies_year['no_of_titles'])
plt.xlabel('release_year')
plt.ylabel('no_of_titles')
plt.show()

but, when I do this I have some errors in place and the key_error shows me 'release_year'. Can I know what is wrong over here as I am new to Python and Matplotlib understanding. Can someone guide me where exactly things are going wrong so that I can correct them next time?

  • Hello, can you edit your question and add a sample of your data ? see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – AlexisG Jan 31 '22 at 08:46
  • @AlexisG - I have added the Snip of the sample data as an image, you can have a look at it. – Scribbled Mind Jan 31 '22 at 08:53
  • 1
    Please use text instead of an image. It's easier with a text, to reproduce the error you encounter – AlexisG Jan 31 '22 at 08:55
  • @AlexisG - I'd do that from next time, by text you meant to say tabular format right ? – Scribbled Mind Jan 31 '22 at 10:08
  • tabular text is better, but the best is an example dataframe that we can directly run. For example : `df = pd.DataFrame([[1, 2])` – AlexisG Jan 31 '22 at 13:16

1 Answers1

0

When doing a group_by, the column "release_year" no longer exist in you Dataframe, since it's now the index.

You have multiple solution :


using a reset_index as you did, but you should reattribute it to your variable

count_movies_year = count_movies_year.reset_index()

or use the inplace parameter

count_movies_year.reset_index(inplace=True)

use the .index directly in your plot

plt.bar(count_movies_year.index, count_movies_year['no_of_titles'])
AlexisG
  • 2,476
  • 3
  • 11
  • 25