Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Pandas Index.is_categorical()

  • Last Updated : 17 Dec, 2018

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 pd
import pandas as pd
  
# Creating the categorical Index
idx = pd.Index(['Labrador', 'Beagle', 'Mastiff', 'Lhasa',
                    'Husky', 'Beagle']).astype('category')
  
# Print the Index
idx

Output :

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 pd
import pandas as pd
  
# Creating the Index
idx = pd.Index(['2015-10-31', '2015-12-02', None, '2016-01-03',
                      '2016-02-08', '2017-05-05', '2014-02-11'])
  
# Print the Index
idx

Output :

Now we check if the labels in the idx are categorical or not.




# test whether idx is having categorical values.
idx.is_categorical()

Output :

As we can see in the output, the function has returned False indicating that the values are not categorical in the idx Index.




My Personal Notes arrow_drop_up
Recommended Articles
Page :