Pandas cut() Method with Example

Learn about the Python Pandas cut() method, its usages, explanation, and examples. By Pranit Sharma Last updated : September 29, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Python pandas.cut() Method

Pandas pandas.cut() method is used to cut the series elements into different bins. The pandas.cut() method is mainly used to carry out statistical analysis.

Syntax

The syntax of pandas.cut() method is:

pandas.cut(
    x, 
    bins, 
    right=True, 
    labels=None, 
    retbins=False, 
    precision=3, 
    include_lowest=False, 
    duplicates='raise', 
    ordered=True
    )

Parameter(s)

The parameters of pandas.cut() method are:

  • X: The array or series whose partitions has to be made.
  • bins: number of bins in which the array or series has to be divided.
  • right: indicates rightmost bins.
  • left: indicates leftmost bins.

Python pandas.cut() Method Example

Suppose, we have a dataframe with multiple columns now each of the columns of this dataframe will act as a series of an array where if we apply the pandas.cut() method and pass the number of bins we want to create, it will divide the array or column into that specific bins.

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d1 = {'One':[i for i in range(10,100,10)]}

# Creating DataFrame
df = pd.DataFrame(d1)

# Display the DataFrame
print("Original DataFrame:\n",df,"\n")

# Using cut method
df['bins'] = pd.cut(df['One'],5)

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Pandas cut() Method Example Output

Reference: pandas.cut()

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.