Get a list of categories of categorical variable (Python Pandas)
Try executing the below code.
List_Of_Categories_In_Column=list(df['Categorical Column Name'].value_counts().index)
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()
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]