Python 给定一个表示值频率的熊猫系列,如何将这些频率转换为百分比?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14281871/
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-08-18 10:58:13  来源:igfitidea点击:

Given a pandas Series that represents frequencies of a value, how can I turn those frequencies into percentages?

pythonpandas

提问by Tim Stewart

I was experimenting with the kaggle.com Titanic data set (data on every person on the Titanic) and came up with a gender breakdown like this:

我正在尝试使用 kaggle.com 泰坦尼克号数据集(泰坦尼克号上每个人的数据),并提出了这样的性别细分:

gender = df.sex.value_counts()
gender

male   577
female 314 

I would like to find out the percentage of each gender on the Titanic.

我想找出泰坦尼克号上每个性别的百分比。

My approach is slightly less than ideal:

我的方法有点不太理想:

from __future__ import division
pcts = gender / gender.sum()
pcts

male      0.647587
female    0.352413

Is there a better (more idiomatic) way?

有没有更好(更惯用)的方式?

Thanks!

谢谢!

采纳答案by fanfabbb

This function is implemented in pandas, actually even in value_counts(). No need to calculate :)

这个函数是在 Pandas 中实现的,实际上甚至在 value_counts() 中。无需计算:)

just type:

只需输入:

df.sex.value_counts(normalize=True)

which gives exactly the desired output.

这正是所需的输出。

Please note that value_counts() excludes NA values, so numbers might not add up to 1. See here: http://pandas-docs.github.io/pandas-docs-travis/generated/pandas.Series.value_counts.html(A column of a DataFrame is a Series)

请注意 value_counts() 不包括 NA 值,因此数字加起来可能不会等于 1。请参见此处:http: //pandas-docs.github.io/pandas-docs-travis/generated/pandas.Series.value_counts.html( DataFrame 的一列是一个系列)

回答by Andy Hayden

I think I would probably do this in one go (without importing division):

我想我可能会一口气做到这一点(不导入部门):

1. * df.sex.value_counts() / len(df.sex)

or perhaps, remembering you want a percentage:

或者,记住你想要一个百分比:

100. * df.sex.value_counts() / len(df.sex)

Much of a muchness really, your way looks fine too.

真的很多很多,你的方式看起来也不错。

回答by Layla

If you want to merge counts with percentage, can use:

如果要将计数与百分比合并,可以使用:

c = df.sex.value_counts(dropna=False)
p = df.sex.value_counts(dropna=False, normalize=True)
pd.concat([c,p], axis=1, keys=['counts', '%'])

回答by Shahar

I know it is an old post, but I hope this answer's gonna help someone in the future. In case you wish to show percentage one of the things that you might do is use value_counts(normalize=True)as answered above by @fanfabbb.

我知道这是一个旧帖子,但我希望这个答案将来会对某人有所帮助。如果您希望显示您可能做的事情的百分比,请使用value_counts(normalize=True)@fanfabbb 上面的回答。

With that said, for many purposes, you might want to show it in the percentage out of a hundred. that can be achieved like so

话虽如此,出于多种目的,您可能希望以百分数显示它。可以这样实现

gender = df.sex.value_counts(normalize=True).mul(100).round(1).astype(str) + '%'

In this case, we multiply the results by hundred, round it to one decimal point and add the percentage sign.

在这种情况下,我们将结果乘以百,四舍五入到一位小数并加上百分号。

Hope it could help:)

希望它可以帮助:)