Home > Backend Development > Python Tutorial > How to Calculate Group-wise Statistics (Count, Mean, etc.) in Pandas GroupBy?

How to Calculate Group-wise Statistics (Count, Mean, etc.) in Pandas GroupBy?

Barbara Streisand
Release: 2024-12-28 04:36:10
Original
600 people have browsed it

How to Calculate Group-wise Statistics (Count, Mean, etc.) in Pandas GroupBy?

Get Statistics for Each Group (Count, Mean, etc) Using Pandas GroupBy

Problem:

You have a DataFrame df in Pandas and want to compute group-wise statistics such as mean, count, and more on multiple columns.

Quick Answer:

To get row counts per group, simply call .size(), which returns a Series:

df.groupby(['col1','col2']).size()
Copy after login

For a DataFrame result with counts as a column, use:

df.groupby(['col1', 'col2']).size().reset_index(name='counts')
Copy after login
Copy after login

Detailed Example:

Consider the DataFrame df:

  col1 col2  col3  col4  col5  col6
0    A    B  0.20 -0.61 -0.49  1.49
1    A    B -1.53 -1.01 -0.39  1.82
2    A    B -0.44  0.27  0.72  0.11
3    A    B  0.28 -1.32  0.38  0.18
4    C    D  0.12  0.59  0.81  0.66
5    C    D -0.13 -1.65 -1.64  0.50
6    C    D -1.42 -0.11 -0.18 -0.44
7    E    F -0.00  1.42 -0.26  1.17
8    E    F  0.91 -0.47  1.35 -0.34
9    G    H  1.48 -0.63 -1.14  0.17
Copy after login

Getting Row Counts:

df.groupby(['col1', 'col2']).size()
Copy after login

Output:

col1  col2
A     B       4
C     D       3
E     F       2
G     H       1
dtype: int64
Copy after login

Creating a DataFrame with Counts:

df.groupby(['col1', 'col2']).size().reset_index(name='counts')
Copy after login
Copy after login

Output:

  col1 col2  counts
0    A    B       4
1    C    D       3
2    E    F       2
3    G    H       1
Copy after login

Including Results for More Statistics:

To calculate additional statistics like mean, median, and min, use agg():

(df
.groupby(['col1', 'col2'])
.agg({
    'col3': ['mean', 'count'],
    'col4': ['median', 'min', 'count']
}))
Copy after login

Output:

            col4                  col3      
          median   min count      mean count
col1 col2                                   
A    B    -0.810 -1.32     4 -0.372500     4
C    D    -0.110 -1.65     3 -0.476667     3
E    F     0.475 -0.47     2  0.455000     2
G    H    -0.630 -0.63     1  1.480000     1
Copy after login

Splitting Statistics into Individual Aggregations:

For more control over the output, split the statistics and combine them using join:

gb = df.groupby(['col1', 'col2'])
counts = gb.size().to_frame(name='counts')
(counts
 .join(gb.agg({'col3': 'mean'}).rename(columns={'col3': 'col3_mean'}))
 .join(gb.agg({'col4': 'median'}).rename(columns={'col4': 'col4_median'}))
 .join(gb.agg({'col4': 'min'}).rename(columns={'col4': 'col4_min'}))
 .reset_index()
)
Copy after login

Output:

  col1 col2  counts  col3_mean  col4_median  col4_min
0    A    B       4  -0.372500       -0.810     -1.32
1    C    D       3  -0.476667       -0.110     -1.65
2    E    F       2   0.455000        0.475     -0.47
3    G    H       1   1.480000       -0.630     -0.63
Copy after login

The above is the detailed content of How to Calculate Group-wise Statistics (Count, Mean, etc.) in Pandas GroupBy?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template