[英]pandas dataframe binned data plotting
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). 我有一个熊猫数据框(df),列df.age的范围是0-100年,列df.haircolor(亮,暗,紫色,灰色)。
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.