Pandas Very Simple Percent of total size from Group by

后端 未结 3 1362
不知归路
不知归路 2021-01-15 11:08

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

相关标签:
3条回答
  • 2021-01-15 11:43

    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
    
    0 讨论(0)
  • 2021-01-15 11:49

    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]
    
    0 讨论(0)
  • 2021-01-15 11:51

    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)

    0 讨论(0)
提交回复
热议问题