Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Creating a histogram from string counts

Tags:

I need to create a histogram from a dataframe column that contains the values "Low', 'Medium', or 'High'. When I try to do the usual df.column.hist(), i get the following error.

ex3.Severity.value_counts() Out[85]:  Low       230 Medium     21 High       16 dtype: int64  ex3.Severity.hist()   TypeError                                 Traceback (most recent call last) <ipython-input-86-7c7023aec2e2> in <module>() ----> 1 ex3.Severity.hist()  C:\Users\C06025A\Anaconda\lib\site-packages\pandas\tools\plotting.py in hist_series(self, by, ax, grid, xlabelsize, xrot, ylabelsize, yrot, figsize, bins, **kwds) 2570         values = self.dropna().values 2571  ->2572         ax.hist(values, bins=bins, **kwds) 2573         ax.grid(grid) 2574         axes = np.array([ax])  C:\Users\C06025A\Anaconda\lib\site-packages\matplotlib\axes\_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs) 5620             for xi in x: 5621                 if len(xi) > 0: ->5622                     xmin = min(xmin, xi.min()) 5623                     xmax = max(xmax, xi.max()) 5624             bin_range = (xmin, xmax)  TypeError: unorderable types: str() < float() 
like image 704
Kyle Avatar asked Apr 08 '15 20:04

Kyle


People also ask

How do you make 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.

What does DF [' A '] Value_counts () do?

value_counts. Return a Series containing counts of unique rows in the DataFrame.

How do I plot a histogram in all columns in R?

To create histogram of all columns in an R data frame, we can use hist. data. frame function of Hmisc package. For example, if we have a data frame df that contains five columns then the histogram for all the columns can be created by using a single line code as hist.


2 Answers

ex3.Severity.value_counts().plot(kind='bar') 

Is what you actually want.

When you do:

ex3.Severity.value_counts().hist() 

it gets the axes the wrong way round i.e. it tries to partition your y axis (counts) into bins, and then plots the number of string labels in each bin.

like image 137
Joe Avatar answered Sep 20 '22 13:09

Joe


Just an updated answer (as this comes up a lot.) Pandas has a nice module for styling dataframes in many ways, such as the case mentioned above....

ex3.Severity.value_counts().to_frame().style.bar()

...will print the dataframe with bars built-in (as sparklines, using excel-terminology). Nice for quick analysis on jupyter notebooks.

see pandas styling docs

like image 30
Ouyang Ze Avatar answered Sep 17 '22 13:09

Ouyang Ze