pandas 以百分比的形式获取列中项目出现的频率 [英] Pandas get frequency of item occurrences in a column as percentage

查看:52
本文介绍了 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, F, or Other. I want to get the percentage of M, F, Other values in the df.

我已经尝试过了,这给了我M,F,Other实例,但是我希望这些占df中值总数的百分比.

I have tried this, which gives me the number M, F, Other instances, but I want these as a percentage of the total number of values in the df.

df.groupby('gender').size()

有人可以帮忙吗?

推荐答案

normalize = True 中使用 value_counts :

df['gender'].value_counts(normalize=True) * 100

结果是(0,1]范围内的分数.在这里我们乘以100以获得%.

The result is a fraction in range (0, 1]. We multiply by 100 here in order to get the %.

这篇关于 pandas 以百分比的形式获取列中项目出现的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆