Python | Панды Series.factorize ()

Опубликовано: 27 Марта, 2022

Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.

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.

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(["New York", "Chicago", "Toronto", None, "Rio"])
  
# Create the Index
sr.index = ["City 1", "City 2", "City 3", "City 4", "City 5"
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

Выход :


Now we will use Series.factorize() function to encode the underlying data of the given series object.

# encode the values
result = sr.factorize()
  
# Print the result
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.

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([80, 25, 3, 80, 24, 25])
  
# Create the Index
index_ = ["Coca Cola", "Sprite", "Coke", "Fanta", "Dew", "ThumbsUp"]
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

Выход :

Now we will use Series.factorize() function to encode the underlying data of the given series object.

# encode the values
result = sr.factorize()
  
# Print the result
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.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.