Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 439
Tim Stewart Avatar asked Jan 10 '13 00:01

Tim Stewart


People also ask

How do you plot a histogram in Python Pandas?

In order to plot a histogram using pandas, chain the . hist() function to the dataframe. This will return the histogram for each numeric column in the pandas dataframe.

How do you plot a value on a histogram?

Create HistogramThe hist() function will use an array of numbers to create a histogram, the array is sent into the function as an argument. For simplicity we use NumPy to randomly generate an array with 250 values, where the values will concentrate around 170, and the standard deviation is 10.

What is bins in Pandas histogram?

Bins are the buckets that your histogram will be grouped by. On the back end, Pandas will group your data into bins, or buckets. Then pandas will count how many values fell into that bucket, and plot the result.


1 Answers

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
like image 139
Andy Hayden Avatar answered Oct 12 '22 02:10

Andy Hayden