Coding Ref

How to use qcut() in Pandas

How to use qcut() in Pandas

In Python, the qcut() function is used to divide a series of values into equal-sized bins. This function is part of the pandas library, which is a popular library for data analysis and manipulation in Python.

Here is an example of how to use the qcut() function in Python:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})

# divide the values in column A into 3 equal-sized bins
binned = pd.qcut(df['A'], 3)

# display the result
print(binned)
output
0    (0.999, 4.0]
1    (0.999, 4.0]
2    (0.999, 4.0]
3    (0.999, 4.0]
4      (4.0, 7.0]
5      (4.0, 7.0]
6      (4.0, 7.0]
7     (7.0, 10.0]
8     (7.0, 10.0]
9     (7.0, 10.0]
Name: A, dtype: category
Categories (3, interval[float64, right]): [(0.999, 4.0] < (4.0, 7.0] < (7.0, 10.0]]

This code will divide the values in column A of the dataframe into 3 equal-sized bins.

You'll also like

Related tutorials curated for you

    How to give multiple conditions in loc() in Pandas

    How to calculate covariance in Pandas

    What is Pandas Cumsum()?

    How to calculate the variance in Pandas DataFrame

    How to sort a series in Pandas

    What does factorize() do in Pandas?

    How to convert a series to a NumPy array in Pandas

    How to create a bar chart in Pandas

    How to groupby mean in Pnadas

    How to convert a Pandas Index to a List

    How to convert a series to a list in Pandas

    How to use qcut() in Pandas