Calculate Quantiles by Group in pandas DataFrame in Python (2 Examples)

On this page you’ll learn how to compute quantiles in the columns of a pandas DataFrame by group in the Python programming language.

Preparing the Examples

import pandas as pd                                      # Import pandas library in Python
my_df = pd.DataFrame({'A':range(110, 122),               # Construct pandas DataFrame
                      'B':[6, 3, 5, 2, 3, 7, 4, 6, 4, 8, 6, 5],
                      'C':range(0, 12),
                      'GRP_a':['C', 'B', 'B', 'A', 'C', 'A', 'A', 'B', 'C', 'A', 'C', 'B'],
                      'GRP_b':['x', 'x', 'x', 'x', 'x', 'x', 'x', 'y', 'y', 'y', 'y', 'y']})
print(my_df)
#       A  B   C GRP_a GRP_b
# 0   110  6   0     C     x
# 1   111  3   1     B     x
# 2   112  5   2     B     x
# 3   113  2   3     A     x
# 4   114  3   4     C     x
# 5   115  7   5     A     x
# 6   116  4   6     A     x
# 7   117  6   7     B     y
# 8   118  4   8     C     y
# 9   119  8   9     A     y
# 10  120  6  10     C     y
# 11  121  5  11     B     y

Example 1: Calculate Quantiles by Single Group

print(my_df.groupby('GRP_a').quantile(0.01))             # Find first percentile by group
#             A     B     C
# GRP_a                    
# A      113.06  2.06  3.06
# B      111.03  3.06  1.03
# C      110.12  3.03  0.12

Example 2: Calculate Quantiles by Group & Subgroup

print(my_df.groupby(['GRP_a', 'GRP_b']).quantile(0.25))  # Find first quartile by multiple groups
#                   A     B     C
# GRP_a GRP_b                    
# A     x      114.00  3.00  4.00
#       y      119.00  8.00  9.00
# B     x      111.25  3.50  1.25
#       y      118.00  5.25  8.00
# C     x      111.00  3.75  1.00
#       y      118.50  4.50  8.50

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top