Home »
Python »
Python Programs
Pandas factorize() Method with Example
Learn about the Python pandas.factorize() method, its usages, explanation, and examples.
By Pranit Sharma Last updated : September 30, 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.factorize() Method
The pandas.factorize() method helps to get the numeric representation of an array by recognizing distinct values. This method is available as both pandas.factorize() and Series.factorize().
Syntax
The syntax of pandas.factorize() method is:
pandas.factorize(
values,
sort=False,
na_sentinel=- 1,
size_hint=None
)
Parameter(s)
The parameters of pandas.factorize() method are:
- values: any sequence
- sort: bool value, by default it is false
- na_sentinel: integer value, by default -1
- size_hint: Hint to the hashtable sizer
Return Value
The return value of pandas.factorize() method is Numeric representation of series.
Python pandas.factorize() Method Example
# Importing pandas package
import pandas as pd
# Creating a series
s = pd.Series(['Ram','Shyam','Seeta','Geeta','Radha','Mohan'])
# Assigning index
s.index = [1,2,3,4,5,6]
# Display the Series
print("Original Series:\n",s,"\n")
# Using factorize
result = s.factorize()
# Display result
print("Result:\n",result)
Output
The output of the above program is:
Reference: pandas.factorize()
Python Pandas Programs »