Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas plot sum of occurrence of string

Hi hoping someone can help. I have a data frame where one of the columns contains a list of names. These names are repeated in some circumstances but not all. I am trying to plot a graph where the x-axis contains the name and then the y-axis contains the number of times that name appears in the column.

I have used the following to count the number of time each name appears.

df.groupby('name').name.count()

Then tried to use the following to plot the graph. However, I get a key error messasge.

df.plot.bar(x='name', y=df.groupby('name').name.count())

Anyone able to tell me what I am doing wrong?

Thanks

like image 760
SeagullWardy Avatar asked Dec 13 '25 17:12

SeagullWardy


1 Answers

I believe you need plot Series returned from count function by Series.plot.bar:

df.groupby('name').name.count().plot.bar()

Or use value_counts:

df['name'].value_counts().plot.bar()
like image 60
jezrael Avatar answered Dec 16 '25 14:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!