Plotting histograms from grouped data in a pandas DataFrame

dreme picture dreme · Oct 25, 2013 · Viewed 115.2k times · Source

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.

Answer

dreme picture dreme · Oct 26, 2013

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