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
Append new categories
To append new categories, use the add_categories() method in Python Pandas. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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