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

Mona picture Mona · Sep 19, 2018 · Viewed 35.3k times · Source

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!

Answer

jezrael picture jezrael · Sep 19, 2018

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]