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.

1 Answer 1

2

Try this:

df['decade'] = df.age // 10 * 10
df.groupby(['decade', 'haircolor']).haircolor.count().plot(kind='bar')
Sign up to request clarification or add additional context in comments.

4 Comments

That gives me a KeyError: 'decades'
Sorry, I thought you already had included the decade in your DataFrame. See above for calculation.
Thanks Alexander, that works - do you know a way to colorcode the different haircolors too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.