Python | Pandas Index.is_categorical ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas Index.is_categorical() function checks if the index holds categorical data. Categorical variables represent types of data which may be divided into groups. Examples of categorical variables are race, sex, age group, and educational level.
Syntax: Index.is_categorical()
Parameters : Doesn’t take any parameter.
Returns : True if the Index is categorical.
Example #1: Use Index.is_categorical() function to check if the input Index is categorical or not.
# importing pandas as pdimport pandas as pd # Creating the categorical Indexidx = pd.Index(["Labrador", "Beagle", "Mastiff", "Lhasa", "Husky", "Beagle"]).astype("category") # Print the Indexidx |
Выход :
Now we find if idx labels are categorical or not.
# Find whether idx1 is categorical or not.idx.is_categorical() |
Output :
The function has returned true indicating that the values contained in the index are categorical.
Example #2: Use Index.is_categorical() function to find if the values contained in the index is categorical or not.
# importing pandas as pdimport pandas as pd # Creating the Indexidx = pd.Index(["2015-10-31", "2015-12-02", None, "2016-01-03", "2016-02-08", "2017-05-05", "2014-02-11"]) # Print the Indexidx |
Выход :
Now we check if the labels in the idx are categorical or not.
# test whether idx is having categorical values.idx.is_categorical() |
Выход :
As we can see in the output, the function has returned False indicating that the values are not categorical in the idx Index.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.