w3resource

Pandas: Data Manipulation - qcut() function

qcut() function

Bin values into discrete intervals.

Quantile-based discretization function. Discretize variable into equal-sized buckets based on rank or based on sample quantiles. For example 1000 values for 10 quantiles would produce a Categorical object indicating quantile membership for each data point.

Syntax:

pandas.qcut(x, q, labels=None, retbins=False, precision=3, duplicates='raise')

Parameters:

Name Description Type Required / Optional
x 1d ndarray or Series Required
q Number of quantiles. 10 for deciles, 4 for quartiles, etc. Alternately array of quantiles, e.g. [0, .25, .5, .75, 1.] for quartiles integer or array of quantiles Required
labels Used as labels for the resulting bins. Must be of the same length as the resulting bins. If False, return only integer indicators of the bins. array or boolean ,default None Required
retbins Whether to return the bins or not. Can be useful if bins is given as a scalar. bool Optional
precision The precision at which to store and display the bins labels. int Optional
duplicates If bin edges are not unique, raise ValueError or drop non-uniques. {default ‘raise’, ‘drop’}, optional

Returns: out : Categorical, Series, or array of integers if labels is False
The return type (Categorical or Series) depends on the input: a Series of type category if input is a Series else Categorical. Bins are represented as categories when categorical data is returned.

bins : ndarray of floats
Returned only if retbins is True.

Notes: Out of bounds values will be NA in the resulting Categorical object.

Example:


Download the above Notebook from here.

Previous: cut() function
Next: merge() function