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.
Try this:
df['decade'] = df.age // 10 * 10
df.groupby(['decade', 'haircolor']).haircolor.count().plot(kind='bar')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.