Pandas function qcut() in Python
The Pandas function qcut() used to discretize variable into equal-sized buckets based on rank or based on sample quantiles. It can also segregate an array of elements into separate bins which is applicable only with one dimension array. It returns two objects as output that is result in bins.
Syntax
 1 Pandas.qcut(x, q, labels = None, retbins = False, precision = 3, duplicates = 'raise')
x : It is a 1D array or Series to discretize buckets.
q : It is an integer or list of float that specifies the number of quantiles.
labels : It is an array or False that used as labels for the resulting bins that must be the same length as the resulting bins. If False, return only integer indicators of the bins. If True, raises an error.
retbins : It expects a boolean value that determine whether to return the bins or not. It used when bins are provided as a scalar value. If not specified, the default value will be False.
include_lowest : It expects a boolean value that used to determine whether the first interval should be left-inclusive or not.
duplicates : It determines whether to raise a ValueError or drop duplicate values if the bin edges are not unique.
qcut() function
 1 import pandas as pd
 2 
 3 res = pd.qcut(range(7), 4)
 4 print('The result of qcut() function :')
 5 print(res)
In the above example, a qcut() function is called with array and quantiles that creates bins that assign to the variable that will be printed on console.
Output
 1 The result of qcut() function :
 2 [(-0.001, 1.5], (-0.001, 1.5], (1.5, 3.0], (1.5, 3.0],
 3  (3.0, 4.5], (4.5, 6.0], (4.5, 6.0]]
 4 
 5 Categories (4, interval[float64, right]):
 6  [(-0.001, 1.5] < (1.5, 3.0] < (3.0, 4.5] < (4.5, 6.0]]

qcut() with labels

qcut() with labels
 1 import pandas as pd
 2 
 3 res = pd.qcut(range(7), 4, labels=['A', 'B', 'C', 'D'])
 4 print('The result of qcut() function :')
 5 print(res)
In the above example, a qcut() function is called that creates bins with labels that assign to the variable that will be printed on console.
Output
 1 The result of qcut() function :
 2 ['A', 'A', 'B', 'B', 'C', 'D', 'D']
 3 Categories (4, object): ['A' < 'B' < 'C' < 'D']

qcut() with labels as False

qcut() with labels as False
 1 import pandas as pd
 2 
 3 res = pd.qcut(range(7), 4, labels = False)
 4 print('The result of qcut() function :')
 5 print(res)
In the above example, a qcut() function is called that creates bins without labels that return an array of numbers.
Output
 1 The result of qcut() function :
 2 [0 0 1 1 2 3 3]
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us