Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting histograms from grouped data in a pandas DataFrame

I need some guidance in working out how to plot a block of histograms from grouped data in a pandas dataframe. Here's an example to illustrate my question:

from pandas import DataFrame import numpy as np x = ['A']*300 + ['B']*400 + ['C']*300 y = np.random.randn(1000) df = DataFrame({'Letter':x, 'N':y}) grouped = df.groupby('Letter') 

In my ignorance I tried this code command:

df.groupby('Letter').hist() 

which failed with the error message "TypeError: cannot concatenate 'str' and 'float' objects"

Any help most appreciated.

like image 478
dreme Avatar asked Oct 25 '13 07:10

dreme


People also ask

How do you plot a histogram in pandas DataFrame?

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 histogram for a dataset in Python?

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables. The matplotlib. pyplot.


1 Answers

I'm on a roll, just found an even simpler way to do it using the by keyword in the hist method:

df['N'].hist(by=df['Letter']) 

That's a very handy little shortcut for quickly scanning your grouped data!

For future visitors, the product of this call is the following chart: enter image description here

like image 171
dreme Avatar answered Sep 29 '22 03:09

dreme