How can I plot a histogram in pandas using nominal values?

Tim Stewart picture Tim Stewart · Jan 10, 2013 · Viewed 9.7k times · Source

Given:

ser = Series(['one', 'two', 'three', 'two', 'two'])

How do I plot a basic histogram of these values?

Here is an ASCII version of what I'd want to see in matplotlib:

     X
 X   X   X
-------------
one two three

I'm tired of seeing:

TypeError: cannot concatenate 'str' and 'float' objects

Answer

Andy Hayden picture Andy Hayden · Jan 10, 2013

You could use the value_counts method:

In [10]: ser.value_counts()
Out[10]: 
two      3
one      1
three    1

and then plot this as a bar chart:

ser.value_counts().plot(kind='bar')

Edit: I noticed that this doesn't keep the desired order. If you have a list/Series for this ordering (in this case ser[:3] will do) you can reindex before plotting:

In [12]: ser.value_counts().reindex(ser[:3])
Out[12]: 
one      1
two      3
three    1