列中按值表示的 pandas 百分比 [英] Pandas percentage by value in a column
本文介绍了列中按值表示的 pandas 百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在df列中获取特定值的百分比.假设我有一个(fol,col2,col3,性别)df性别列,其值是M或F.我想获取df中M和F值的百分比.
I want to get a percentage of a particular value in a df column. Say I have a df with (col1, col2 , col3, gender) gender column has values of M or F. I want to get the percentage of M and F values in the df.
我已经尝试过了,这给了我M和F个实例,但是我希望它们占df中值总数的百分比.
I have tried this, which gives me the number M and F instances, but I want these as a percentage of the total number of values in the df.
df.groupby('gender').size()
有人可以帮忙吗?
推荐答案
将value_counts
与normalize=True
一起使用:
df['gender'].value_counts(normalize=True) * 100
这篇关于列中按值表示的 pandas 百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文