Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

前端 未结 7 1506
无人及你
无人及你 2020-11-21 06:06

I have a data frame df and I use several columns from it to groupby:

df[\'col1\',\'col2\',\'col3\',\'col4\'].groupby([\'col1\',\'co         


        
7条回答
  •  孤城傲影
    2020-11-21 06:32

    Swiss Army Knife: GroupBy.describe

    Returns count, mean, std, and other useful statistics per-group.

    df.groupby(['A', 'B'])['C'].describe()
    
               count  mean   std   min   25%   50%   75%   max
    A   B                                                     
    bar one      1.0  0.40   NaN  0.40  0.40  0.40  0.40  0.40
        three    1.0  2.24   NaN  2.24  2.24  2.24  2.24  2.24
        two      1.0 -0.98   NaN -0.98 -0.98 -0.98 -0.98 -0.98
    foo one      2.0  1.36  0.58  0.95  1.15  1.36  1.56  1.76
        three    1.0 -0.15   NaN -0.15 -0.15 -0.15 -0.15 -0.15
        two      2.0  1.42  0.63  0.98  1.20  1.42  1.65  1.87
    

    To get specific statistics, just select them,

    df.groupby(['A', 'B'])['C'].describe()[['count', 'mean']]
    
               count      mean
    A   B                     
    bar one      1.0  0.400157
        three    1.0  2.240893
        two      1.0 -0.977278
    foo one      2.0  1.357070
        three    1.0 -0.151357
        two      2.0  1.423148
    

    describe works for multiple columns (change ['C'] to ['C', 'D']—or remove it altogether—and see what happens, the result is a MultiIndexed columned dataframe).

    You also get different statistics for string data. Here's an example,

    df2 = df.assign(D=list('aaabbccc')).sample(n=100, replace=True)
    
    with pd.option_context('precision', 2):
        display(df2.groupby(['A', 'B'])
                   .describe(include='all')
                   .dropna(how='all', axis=1))
    
                  C                                                   D                
              count  mean       std   min   25%   50%   75%   max count unique top freq
    A   B                                                                              
    bar one    14.0  0.40  5.76e-17  0.40  0.40  0.40  0.40  0.40    14      1   a   14
        three  14.0  2.24  4.61e-16  2.24  2.24  2.24  2.24  2.24    14      1   b   14
        two     9.0 -0.98  0.00e+00 -0.98 -0.98 -0.98 -0.98 -0.98     9      1   c    9
    foo one    22.0  1.43  4.10e-01  0.95  0.95  1.76  1.76  1.76    22      2   a   13
        three  15.0 -0.15  0.00e+00 -0.15 -0.15 -0.15 -0.15 -0.15    15      1   c   15
        two    26.0  1.49  4.48e-01  0.98  0.98  1.87  1.87  1.87    26      2   b   15
    

    For more information, see the documentation.


    pandas >= 1.1: DataFrame.value_counts

    This is available from pandas 1.1 if you just want to capture the size of every group, this cuts out the GroupBy and is faster.

    df.value_counts(subset=['col1', 'col2'])
    

    Minimal Example

    # Setup
    np.random.seed(0)
    df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                              'foo', 'bar', 'foo', 'foo'],
                       'B' : ['one', 'one', 'two', 'three',
                              'two', 'two', 'one', 'three'],
                       'C' : np.random.randn(8),
                       'D' : np.random.randn(8)})
    
    df.value_counts(['A', 'B']) 
    
    A    B    
    foo  two      2
         one      2
         three    1
    bar  two      1
         three    1
         one      1
    dtype: int64
    

    Other Statistical Analysis Tools

    If you didn't find what you were looking for above, the User Guide has a comprehensive listing of supported statical analysis, correlation, and regression tools.

提交回复
热议问题