python-3.x - 查找男性和女性的百分比
问题描述
在下面的代码之后,我能够从数据集中获得计数:
Users2 = Users.gender.groupby([Users['occupation'],Users['gender']]).count().astype(int)
Users2
输出:
occupation gender
administrator F 36
M 43
artist F 13
M 15
doctor M 7
educator F 26
M 69
engineer F 2
M 65
但是,我需要男性和女性的百分比而不是计数。
样本数据:
user_id age gender occupation
0 1 24 M doctor
1 2 53 F educator
2 3 23 M writer
3 4 24 M administrator
4 5 33 F artist
解决方案
SeriesGroupBy.value_counts
与 一起使用normalize=True
:
#changed sample data for better MCVE
print (Users)
user_id age gender occupation
0 1 24 M technician
1 2 53 F technician
2 3 23 M writer
3 4 24 M technician
4 5 33 F writer
df = (Users.groupby('occupation')['gender']
.value_counts(normalize=True)
.reset_index(name='perc'))
print (df)
occupation gender perc
0 technician M 0.666667
1 technician F 0.333333
2 writer F 0.500000
3 writer M 0.500000
细节:
#without normalize=True get counts per groups
print (Users.groupby('occupation')['gender']
.value_counts())
occupation gender
technician M 2
F 1
writer F 1
M 1
Name: gender, dtype: int64
#with normalize=True get percentages
print (Users.groupby('occupation')['gender']
.value_counts(normalize=True))
occupation gender
technician M 0.666667
F 0.333333
writer F 0.500000
M 0.500000
Name: gender, dtype: float64
推荐阅读
- python - Python from django.contrib.auth.views import logout ImportError: cannot import name 'logout'
- sed - 使用 sed 删除所有块中的特定行值
- javascript - Angular不显示html页面
- javascript - 在javascript中实现幂函数
- c++ - 将派生对象传递给构造函数时出现错误 C2664
- ruby-on-rails - 所有公寓子域中的 Rails 请求
- swift - 将 dosumentSnapshot 分配给自定义对象?
- python - python中的分布图
- python - 在 Unix 系统上使用 Python Ctypes 函数调用分段错误
- javascript - 如何添加 ScrollMagic 做出反应?