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.
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.
import pandas as pd
idx = pd.Index([ 'Labrador' , 'Beagle' , 'Mastiff' , 'Lhasa' ,
'Husky' , 'Beagle' ]).astype( 'category' )
idx
|
Output :
Now we find if idx labels are categorical or not.
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.
import pandas as pd
idx = pd.Index([ '2015-10-31' , '2015-12-02' , None , '2016-01-03' ,
'2016-02-08' , '2017-05-05' , '2014-02-11' ])
idx
|
Output :
Now we check if the labels in the idx are categorical or not.
Output :
As we can see in the output, the function has returned False
indicating that the values are not categorical in the idx Index.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...