Python | Pandas Series.factorize()
Last Updated :
13 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.factorize()
function encode the object as an enumerated type or categorical variable. This method is useful for obtaining a numeric representation of an array when all that matters is identifying distinct values.
Syntax: Series.factorize(sort=False, na_sentinel=-1)
Parameter :
sort : Sort uniques and shuffle labels to maintain the relationship.
na_sentinel : Value to mark “not found”.
Returns :
labels : ndarray
uniques : ndarray, Index, or Categorical
Example #1: Use Series.factorize()
function to encode the underlying data of the given series object.
import pandas as pd
sr = pd.Series([ 'New York' , 'Chicago' , 'Toronto' , None , 'Rio' ])
sr.index = [ 'City 1' , 'City 2' , 'City 3' , 'City 4' , 'City 5' ]
sr.index = index_
print (sr)
|
Output :
Now we will use Series.factorize()
function to encode the underlying data of the given series object.
result = sr.factorize()
print (result)
|
Output :
As we can see in the output, the Series.factorize()
function has successfully encoded the underlying data of the given series object. Notice missing values has been assigned a code of -1.
Example #2 : Use Series.factorize()
function to encode the underlying data of the given series object.
import pandas as pd
sr = pd.Series([ 80 , 25 , 3 , 80 , 24 , 25 ])
index_ = [ 'Coca Cola' , 'Sprite' , 'Coke' , 'Fanta' , 'Dew' , 'ThumbsUp' ]
sr.index = index_
print (sr)
|
Output :
Now we will use Series.factorize()
function to encode the underlying data of the given series object.
result = sr.factorize()
print (result)
|
Output :
As we can see in the output, the Series.factorize()
function has successfully encoded the underlying data of the given series object.
Share your thoughts in the comments
Please Login to comment...