pandas 如何用熊猫绘制年龄分布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38146009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:30:24  来源:igfitidea点击:

How to plot age distribution with pandas

pythonpandasgroup-bypandas-groupby

提问by dimazubrik

I have Data Frame, which contains 2 columns: age and gender.

我有数据框,其中包含 2 列:年龄和性别。

sex,age
1,30
2,29
1,34
1,27
2,28
2,28
1,40
1,30
1,27
2,31
1,37
1,31
2,28
2,30
2,27
2,27
2,29
2,32
1,28
1,27
1,28
1,28
1,29
1,33
1,32
1,30

How can I plot age distribution for each gender?

如何绘制每个性别的年龄分布?

回答by piRSquared

groupbythen plotwith kind='kde'

groupby然后plotkind='kde'

df1.groupby('sex').age.plot(kind='kde')

enter image description here

在此处输入图片说明

Per @EdChum

每@EdChum

df1.groupby('sex').age.hist()

enter image description here

在此处输入图片说明