将占总数百分比的列添加到 Pandas 数据框

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

Add column for percentage of total to Pandas dataframe

pythonpandasgroup-bypandas-groupby

提问by AlliDeacon

I have a dataframe that I am doing a groupby()on to get the counts on a column's values. I am trying to add an additional column for "Percentage of Total". I'm not sure how to accomplish that.

我有一个数据框,我正在使用groupby()它来获取列值的计数。我正在尝试为“总百分比”添加一个额外的列。我不知道如何做到这一点。

I've looked at a few groupby options, but can't seem to find anything that fits.

我查看了一些 groupby 选项,但似乎找不到任何适合的选项。

My dataframe looks like this:

我的数据框如下所示:

              DAYSLATE
DAYSLATE          
-7 days          1
-5 days          2
-3 days          8
-2 days          9
-1 days         45
0 days         589
1 days          33
2 days           8
3 days          16
4 days          14
5 days          16
6 days           2
7 days           6
8 days           2
9 days           2
10 days          1

回答by piRSquared

Option 1

选项1

df['DAYSLATE_pct'] = df.DAYSLATE / df.DAYSLATE.sum()

Option 2
Use pd.value_countsinstead of groupby

选项 2
使用pd.value_counts代替groupby

pre_df.DAYSLATE.value_counts(normalize=True)