![]() |
VOOZH | about |
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:
Example 1: Calculate quantiles by group
Output:
Example 2: Calculate quantiles by group
Output: