Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset the categories of categorical index in Pandas

Tags:

pandas

I have a dataframe with a column being categorical.

I remove all the rows having one the categories.

How can I make sure the resulting dataframe has only those categories that exist and does not keep the deleted categories in its index?

like image 500
user1780424 Avatar asked Sep 01 '25 22:09

user1780424


1 Answers

df = pd.DataFrame({'color':np.random.choice(['Blue','Green','Brown','Red'], 50)})

df.color = df.color.astype('category')

df.color.head()

Output:

0     Blue
1    Green
2     Blue
3    Green
4    Brown
Name: color, dtype: category
Categories (4, object): [Blue, Brown, Green, Red]

Remove Brown from dataframe and category.

df = df.query('color != "Brown"')

df.color = df.color.cat.remove_categories('Brown')

df.color.head()

Output:

0     Blue
1    Green
2     Blue
3    Green
7      Red
Name: color, dtype: category
Categories (3, object): [Blue, Green, Red]
like image 146
Scott Boston Avatar answered Sep 05 '25 13:09

Scott Boston