Python | Панды TimedeltaIndex.factorize

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

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas TimedeltaIndex.factorize() function encode the elements of the given TimedeltaIndex object as an enumerated type or categorical variable.

Syntax : TimedeltaIndex.factorize(sort=False, na_sentinel=-1)

Parameters :
sort : Sort by values
na_sentinel : Value to mark “not found”

Return : labels : the indexer to the original array

Example #1: Use TimedeltaIndex.factorize() function to encode the elements of the given TimedeltaIndex objects.

# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(start ="1 days 06:05:01.000030",
                   periods = 5, freq ="D", name ="Koala")
  
# Print the TimedeltaIndex object
print(tidx)

Выход :

Now we will use the TimedeltaIndex.factorize() function to encode the tidx object.

# encode the tidx object
tidx.factorize()

Выход :

As we can see in the output, the TimedeltaIndex.factorize() function has encoded the elements of the tidx object. It has assigned a unique code to each element.
 
Example #2: Use TimedeltaIndex.factorize() function to check if the elements contained in the two given TimedeltaIndex objects are same or not.

# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(data =["1 days 02:00:00", "1 days 06:05:01.000030",
          "1 days 02:00:00", "1 days 02:00:00", "21 days 06:15:01.000030"])
  
# Print the TimedeltaIndex object
print(tidx)

Output :

Now we will use the TimedeltaIndex.factorize() function to encode the tidx object.

# encode the tidx object
tidx.factorize()

Output :

As we can see in the output, the TimedeltaIndex.factorize() function has encoded the elements of the tidx object. It has assigned a unique code to each element.

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

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