Mona
Mona

Reputation: 383

Get a list of categories of categorical variable

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.

Upvotes: 38

Views: 120025

Answers (5)

Anand
Anand

Reputation: 309

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()

Upvotes: 11

user3588569
user3588569

Reputation:

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

cat_arr = numpy.array(pd.Categorical(dataframe['col_name']).categories)

Upvotes: 2

Engr M Faysal
Engr M Faysal

Reputation: 149

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)

Upvotes: 1

SABARISH KAVALA
SABARISH KAVALA

Reputation: 41

Try executing the below code.

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

Upvotes: 4

jezrael
jezrael

Reputation: 862406

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]

Upvotes: 47

Related Questions