1

I have a pandas dataframe (df) with a column df.age ranging from 0 - 100 years and a column df.haircolor (bright, dark, purple, grey).

Now I want to bin the age in decades:

bins = np.linspace(df.age.min(), df.age.max(), 10)
decades = df.groupby(np.digitize(df.age, bins))

Now I am trying to find a good way to plot this. I'd like a barplot where each haircolor has a bar. Naively I tried that.

df['haircolor'].plot(kind='bar', by=decades)

It is not giving me the result I hoped for. Anyone an idea? thanks.

Annamarie
  • 183
  • 1
  • 2
  • 13

1 Answers1

2

Try this:

df['decade'] = df.age // 10 * 10
df.groupby(['decade', 'haircolor']).haircolor.count().plot(kind='bar')
Alexander
  • 105,104
  • 32
  • 201
  • 196