VOOZH about

URL: https://www.geeksforgeeks.org/python/python-pandas-categorical/

⇱ Python | Pandas.Categorical() - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Pandas.Categorical()

Last Updated : 10 Mar, 2022

pandas.Categorical(val, categories = None, ordered = None, dtype = None) : It represents a categorical variable. Categorical are a pandas data type that corresponds to the categorical variables in statistics. Such variables take on a fixed and limited number of possible values. For examples - grades, gender, blood group type etc. 
Also, in the case of categorical variables, logical order is not the same as categorical data e.g. "one", "two", "three". But the sorting of these variables uses logical order. 
 

Parameters- val : [list-like] The values of categorical. 
categories : [index like] Unique categorisation of the categories. 
ordered : [boolean] If false, then the categorical is treated as unordered. 
dtype : [CategoricalDtype] an instance. 

Error- ValueError :  If the categories do not validate. 
TypeError :  If an explicit ordered = True but categorical can't be sorted. 

Return- Categorical variable


Code:
 

Output : 
 

👁 Image


  
 

Output : 
 

👁 Image


  
 

Output : 
 

👁 Image


  
 

Output : 
 

👁 Image


 

Comment