Get a list of categories of categorical variable (Python Pandas)

Question:

I have a pandas DataFrame with a column representing a categorical variable. How can I get a list of the categories? I tried .values on the column but that does not return the unique levels.

Thanks!

Asked By: Mona

||

Answers:

I believe need Series.cat.categories or unique:

np.random.seed(1245)

a = ['No', 'Yes', 'Maybe']
df = pd.DataFrame(np.random.choice(a, size=(10, 3)), columns=['Col1','Col2','Col3'])
df['Col1'] = pd.Categorical(df['Col1'])

print (df.dtypes)
Col1    category
Col2      object
Col3      object
dtype: object

print (df['Col1'].cat.categories)
Index(['Maybe', 'No', 'Yes'], dtype='object')

print (df['Col2'].unique())
['Yes' 'Maybe' 'No']

print (df['Col1'].unique())
[Maybe, No, Yes]
Categories (3, object): [Maybe, No, Yes]
Answered By: jezrael

Try executing the below code.

List_Of_Categories_In_Column=list(df['Categorical Column Name'].value_counts().index)

Answered By: SABARISH KAVALA

You can also use value_counts(), but it only works when you use it with a column name, with which you’ll get the counts of each category as well.
Example:

dataframe['Columnn name'].value_counts()

Alternatively, if you want the total count of categories in a variable, you can do this:

dataframe['Columnn name'].value_counts().count()
Answered By: Anand

df.column name.value_counts() # to see total number of values for each categories in a column

df.column name.value_counts().index # to see only the categories name

df.column name .value_counts().count() # to see how many categories in a column (only number)

Answered By: Engr M Faysal

This is working for me to retrieve the categories as an array

cat_arr = numpy.array(pd.Categorical(dataframe['col_name']).categories)
Answered By: KapoNYC
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.