Python | Pandas Index.factorize() Last Updated : 17 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.factorize() function encode the object as an enumerated type or categorical variable. This method is useful for obtaining a numeric representation of an array when all that matters is identifying distinct values. factorize is available as both a top-level function pandas.factorize(), and as a method Series.factorize() and Index.factorize(). Syntax: Index.factorize(sort=False, na_sentinel=-1) Parameters : sort : Sort uniques and shuffle labels to maintain the relationship. na_sentinel : Value to mark “not found”. Returns : An integer ndarray that’s an indexer into uniques. uniques.take(labels) will have the same values as values. Example #1: Use Index.factorize() function to encode the given Index values into categorical form. Python3 # importing pandas as pd import pandas as pd # Creating the Index idx = pd.Index(['Labrador', 'Beagle', 'Labrador', 'Lhasa', 'Husky', 'Beagle']) # Print the Index idx Output : Let's factorize the given Index. Python3 1== # convert it into categorical values. idx.factorize() Output : As we can see in the output, the Index.factorize() function has converted each label in the Index to a category and has assigned them numerical values. Example #2: Use Index.factorize() function to factorize the index values based on their sorted order sequence. Python3 # importing pandas as pd import pandas as pd # Creating the Index idx = pd.Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']) # Print the Index idx Output : Let's factorize it based on sorted order. Numerical values are assigned only after the sorting of the values in the Index. Python3 1== # Factorize the sorted labels idx.factorize(sort = True) Output : As we can see in the output, sorting has been performed on the Index values before assigning them numerical values. Comment More infoAdvertise with us Next Article Python | Pandas Index.all() S Shubham__Ranjan Follow Improve Article Tags : Technical Scripter Python Python-pandas Python pandas-indexing Practice Tags : python Similar Reads Python | Pandas Index.astype() 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.astype() function create an Index with values cast to dtypes. The class o 2 min read Python | Pandas Index.copy() 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.copy() function make a copy of this object. The function also sets the na 2 min read Python | Pandas Index.argsort() 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.argsort() function returns the integer indices that would sort the index. 2 min read Python | Pandas Index.to_frame() 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.to_frame() function create a dataFrame from the given index with a column 2 min read Python | Pandas Index.all() 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.all() function checks if all the elements in the index are true or not. I 2 min read Python | Pandas Index.flags Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.flags attribute return the status of all the flags for the given Index object. Syntax: Index.flags Parameter : None Returns : status o 2 min read Like