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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With