24 Jan Working with Categories in Pandas
In this lesson, we will understand how to work with Categories in Pandas. We will learn the following with examples:
- Append new categories
- Remove a category
Before moving further, we’ve prepared a video tutorial to work with categories data in Pandas:
Append new categories
To append new categories, use the add_categories() method in Python Pandas. Let us see an example:
import pandas as pd
# Creating a Categorical Series
s = pd.Series(["p", "q", "r", "s", "q"], dtype="category")
# Display the Series
print("Series = \n", s)
# Append Category
s = s.cat.add_categories([5])
# Display the updated category
print("\nUpdated Categories = ",s.cat.categories)
Output
Series = 0 p 1 q 2 r 3 s 4 q dtype: category Categories (4, object): [p, q, r, s] Updated Categories = Index(['p', 'q', 'r', 's', 5], dtype='object')
Remove a Category in Pandas
To remove a category, use the remove_categories() method in Python Pandas. Let us see an example:
import pandas as pd
# Creating a Categorical Series
s = pd.Series(["p", "q", "r", "s", "q"], dtype="category")
# Display the Series
print("Series\n", s)
# Remove a Category
# Display the updated category after removing a specific category
print("\nUpdated Categories\n",s.cat.remove_categories("r"))
Output
Series 0 p 1 q 2 r 3 s 4 q dtype: category Categories (4, object): [p, q, r, s] Updated Categories 0 p 1 q 2 NaN 3 s 4 q dtype: category Categories (3, object): [p, q, s]
As you can see above, now we have 3 categories, since we have removed one of the categories.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments