Pandas cut function
pandas.cut()
The function can distribute the given data into a range, which can also be called bins
.
We will use the following DataFrame in this article.
import pandas as pd
df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)
print(df)
Output:
Name Age Score
0 Anish 23 316
1 Birat 34 322
2 Chirag 38 332
3 Kabin 45 330
4 Sachin 27 325
pandas.cut()
Function syntax
pandas.cut(
x,
bins,
right=True,
labels=None,
retbins=False,
precision=3,
include_lowest=False,
duplicates="raise",
ordered=True,
)
parameter
x |
Given an array |
bins |
Data classification standards |
right |
Boolean. If True, also include bins the rightmost digits. |
labels |
Array. bins Labels. |
retbins |
Boolean. If yes True , return bins . |
precision |
Integer type. Storage and display bins precision |
ordered |
Boolean. If true True , the labels of the results will be sorted. |
Return Value
It returns an array representing the value x
of each element in bin
, and if we set it retbins=True
, it will also return it bins
.
Example: Use pandas.cut()
the method to distribute the column values of a DataFrame into bins
import pandas as pd
df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)
print("Initial DataFrame:")
print(df, "\n")
df["Age-Range"] = pd.cut(x=df["Age"], bins=[20, 30, 40, 50])
print("DataFrame with Age-Range:")
print(df)
Output:
Initial DataFrame:
Name Age Score
0 Anish 23 316
1 Birat 34 322
2 Chirag 38 332
3 Kabin 45 330
4 Sachin 27 325
DataFrame with Age-Range:
Name Age Score Age-Range
0 Anish 23 316 (20, 30]
1 Birat 34 322 (30, 40]
2 Chirag 38 332 (30, 40]
3 Kabin 45 330 (40, 50]
4 Sachin 27 325 (20, 30]
It divides the values of the column df
in the DataFrame into age ranges calculated using the value of the parameter in the method and finally displays the value of for each row in the DataFrame.Age
pandas.cut()
bins
Age-Range
Here, (20,30]
it means the values from 20 to 30, excluding 20 and including 30.
Example: Use pandas.cut()
the method to distribute values into different regions and assign a label to each region
By default, the label assigned to each bin will be the range of the bin. We can set custom bin labels using the argument pandas.cut()
in the bin function .labels
import pandas as pd
df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)
print("Initial DataFrame:")
print(df, "\n")
bin_labels = labels = ["21 to 30", "31 to 40", "41 to 50"]
df["Age-Range"] = pd.cut(x=df["Age"], bins=[20, 30, 40, 50], labels=bin_labels)
print("DataFrame with Age-Range:")
print(df)
Output:
Initial DataFrame:
Name Age Score
0 Anish 23 316
1 Birat 34 322
2 Chirag 38 332
3 Kabin 45 330
4 Sachin 27 325
DataFrame with Age-Range:
Name Age Score Age-Range
0 Anish 23 316 21 to 30
1 Birat 34 322 31 to 40
2 Chirag 38 332 31 to 40
3 Kabin 45 330 41 to 50
4 Sachin 27 325 21 to 30
It Age
assigns each value of the column to a different bin and adds a label to each bin.
Example: pandas.cut()
Set in method retbins=True
to return bin value
import pandas as pd
df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)
print("Initial DataFrame:")
print(df, "\n")
bin_labels = labels = ["21 to 30", "31 to 40", "41 to 50"]
df["Age-Range"], bin_values = pd.cut(
x=df["Age"], bins=[20, 30, 40, 50], labels=bin_labels, retbins=True
)
print("DataFrame with Age-Range:")
print(df, "\n")
print("The bin values are:")
print(bin_values)
Output:
Initial DataFrame:
Name Age Score
0 Anish 23 316
1 Birat 34 322
2 Chirag 38 332
3 Kabin 45 330
4 Sachin 27 325
DataFrame with Age-Range:
Name Age Score Age-Range
0 Anish 23 316 21 to 30
1 Birat 34 322 31 to 40
2 Chirag 38 332 31 to 40
3 Kabin 45 330 41 to 50
4 Sachin 27 325 21 to 30
The bin values are:
[20 30 40 50]
It displays Age-Range
the DataFrame with values and bin values.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a
Appending to an Empty DataFrame in Pandas
Publish Date:2025/05/01 Views:54 Category:Python
-
As we learned earlier, Pandas in Python is an open source module that we can use for data analysis and making machine learning models. It is Numpy used along with another package called as they go hand in hand to support multidimensional ar
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:108 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:86 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:61 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:182 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame