Pandas 非常简单 来自 Group by 的总大小百分比

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

Pandas Very Simple Percent of total size from Group by

pythonpandas

提问by horatio1701d

I'm having trouble for a seemingly incredibly easy operation. What is the most succint way to just get a percent of total from a group by operation such as df.groupby['col1'].size(). My DF after grouping looks like this and I just want a percent of total. I remember using a variation of this statement in the past but cannot get this to work now: percent = totals.div(totals.sum(1), axis=0)

我遇到了一个看似非常简单的操作的麻烦。通过诸如df.groupby['col1'].size(). 分组后我的 DF 看起来像这样,我只想要总数的百分比。我记得过去使用过此语句的变体,但现在无法使其正常工作:percent = totals.div(totals.sum(1), axis=0)

Original DF:

原始DF:

       A   B   C
    0  77   3  98
    1  77  52  99
    2  77  58  61
    3  77   3  93
    4  77  31  99
    5  77  53  51
    6  77   2   9
    7  72  25  78
    8  34  41  34
    9  44  95  27

Result:

结果:

df1.groupby('A').size() / df1.groupby('A').size().sum()

    A
    34    0.1
    44    0.1
    72    0.1
    77    0.7

Here is what I came up with so far which seems pretty reasonable way to do this:

到目前为止,这是我想出的似乎很合理的方法:

df.groupby('col1').size().apply(lambda x: float(x) / df.groupby('col1').size().sum()*100)

采纳答案by horatio1701d

Getting good performance (3.73s) on DF with shape (3e6,59) by using: df.groupby('col1').size().apply(lambda x: float(x) / df.groupby('col1').size().sum()*100)

通过使用以下命令在形状为 (3e6,59) 的 DF 上获得良好的性能 (3.73s): df.groupby('col1').size().apply(lambda x: float(x) / df.groupby('col1').size().sum()*100)

回答by Roman Pekar

I don't know if I'm missing something, but looks like you could do something like this:

我不知道我是否遗漏了什么,但看起来你可以做这样的事情:

df.groupby('A').size() * 100 / len(df)

or

或者

df.groupby('A').size() * 100 / df.shape[0]

回答by Alexander

How about:

怎么样:

df = pd.DataFrame({'A': {0: 77, 1: 77, 2: 77, 3: 77, 4: 77, 5: 77, 6: 77, 7: 72, 8: 34, 9: None},
                   'B': {0: 3, 1: 52, 2: 58, 3: 3, 4: 31, 5: 53, 6: 2, 7: 25, 8: 41, 9: 95},
                   'C': {0: 98, 1: 99, 2: 61, 3: 93, 4: 99, 5: 51, 6: 9, 7: 78, 8: 34, 9: 27}})

>>> df.groupby('A').size().divide(sum(df['A'].notnull()))
A
34    0.111111
72    0.111111
77    0.777778
dtype: float64

>>> df
    A   B   C
0  77   3  98
1  77  52  99
2  77  58  61
3  77   3  93
4  77  31  99
5  77  53  51
6  77   2   9
7  72  25  78
8  34  41  34
9 NaN  95  27