Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to Calculate Quantiles by Group in Pandas?

How to Calculate Quantiles by Group in Pandas?

In this article, how to calculate quantiles by group in Pandas using Python.

There are many methods to calculate the quantile, but pandas provide groupby.quantile() function to find it in a simple few lines of code. This is the Method to use when the desired quantile falls between two points.

Syntax: 

DataFrameGroupBy.quantile(self, q=0.5, interpolation=’linear’)

Parameters:  

  • q : float or array-like, default 0.5 (50% quantile) Values are given between 0 and 1 providing the quantiles to compute.
  • Interpolation : {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}

In this method, the values and interpolation are passed as parameters. By default, the q value will be 0.5 and interpolation will be Linear. This returns the series or Dataframe determined by the GroupBy object.

Dataframe in use:

dataframe

Example 1: Calculate quantiles by group

Python3




# Importing libraries
import pandas as pd
  
# Storing data in dictionary
game = {'Player': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
                        'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                        'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'],
        'wins': [2, 4, 4, 5, 6, 9, 13, 13, 15, 15, 14, 13,
                 11, 9, 9, 8, 8, 16, 19, 21, 14, 20, 19, 18]
        }
# Creating data frame
df = pd.DataFrame(game)
  
# calculating quantile
df.groupby('Player').quantile(0.5)


Output:

output

Example 2: Calculate quantiles by group

Python3




# Importing libraries
import pandas as pd
  
# Storing data in dictionary
game = {'Player': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
                        'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                        'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'],
        'wins': [2, 4, 4, 5, 6, 9, 13, 13, 15, 15, 14, 13,
                 11, 9, 9, 8, 8, 16, 19, 21, 14, 20, 19, 18]
        }
# Creating data frame
df = pd.DataFrame(game)
  
# calculating quantile
df.groupby('Player').quantile(0.9)


Output:

output

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments