- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Pandas add_categories() Method
The add_categories() method in pandas is a specialized function used with categorical data. It allows users to add new categories to a pandas categorical object. These new categories are appended at the last/highest position in the existing categories and are initially unused directly after this call.
This method is part of the Pandas Series.cat accessor, and is particularly useful for extending or modifying the set of categories in a categorical Series or CategoricalIndex. Which is particularly useful when expanding the valid categories for categorical data.
Syntax
Below you can see the syntax of the Python Pandas add_categories() method, it differs slightly depending on whether you're working with a Categorical Series or a CategoricalIndex object.
Syntax for a Pandas Categorical Series −
Series.cat.add_categories(new_categories, *args, **kwargs)
Syntax for a CategoricalIndex −
CategoricalIndex.add_categories(new_categories, *args, **kwargs)
When using the add_categories() method on a CategoricalIndex, there is no need to use the .cat accessor. This is because the CategoricalIndex is inherently categorical, and methods can be called directly.
Parameters
The Python Pandas add_categories() method accepts the below parameters −
new_categories − This parameter accepts a single category or a list-like collection of categories to add to the existing set of categories.
**kwargs: Additional key word arguments for more customization.
Return Value
The Pandas add_categories() method returns a Categorical object with the newly added categories appended at the end of the list.
Exception
This method will raise a ValueError if the new categories already exist in the current categories or if the input is invalid.
Example: Adding Single Category
Here is a basic example demonstrates how to add a single new category to a categorical Series using the Pandas Series.cat.add_categories() method.
import pandas as pd
# Creating a categorical Series
s = pd.Series(["apple", "banana", "cherry"], dtype="category")
print("Original Series:")
print(s)
# Adding a new category
s = s.cat.add_categories("date")
print("\nSeries after adding a new category:")
print(s)
When we run above program, it produces following result −
Original Series: 0 apple 1 banana 2 cherry dtype: category Categories (3, object): ['apple', 'banana', 'cherry'] Series after adding a new category: 0 apple 1 banana 2 cherry dtype: category Categories (4, object): ['apple', 'banana', 'cherry', 'date']
Example: Adding Multiple Categories
This example demonstrates how to add multiple new categories to the existing set of categories by providing the list-like collection of categories to the new_categories parameter.
import pandas as pd
# Creating a categorical Series
s = pd.Series(["red", "blue", "green"], dtype="category")
print("Original Series:")
print(s)
# Adding new categories
s = s.cat.add_categories(["yellow", "orange"])
print("\nSeries after adding multiple categories:")
print(s)
While executing the above code we get the following output −
Original Series: 0 red 1 blue 2 green dtype: category Categories (3, object): ['blue', 'green', 'red'] Series after adding multiple categories: 0 red 1 blue 2 green dtype: category Categories (5, object): ['blue', 'green', 'red', 'yellow', 'orange']
Example: Adding Categories to a CategoricalIndex
The following example demonstrates using the add_categories() method for adding new categories to the CategoricalIndex object.
import pandas as pd
# Creating a CategoricalIndex
catIndex = pd.CategoricalIndex(
["p", "q", "r", "s", "p", "q", "r", "s"],
ordered=True,
categories=["p", "q", "r", "s"]
)
print("Original CategoricalIndex:")
print(catIndex)
# Adding new categories
new_index = catIndex.add_categories(["x", "y", "z"])
print("\nCategoricalIndex after adding new categories:")
print(new_index)
Following is an output of the above code −
Original CategoricalIndex: CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category') CategoricalIndex after adding new categories: CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's', 'x', 'y', 'z'], ordered=True, dtype='category')
Example: Handling Exceptions When Adding Categories
This example demonstrates the behavior of the add_categories() method when you try to add duplicate categories.
import pandas as pd
# Creating a categorical Series
s = pd.Series(["apple", "banana", "cherry"], dtype="category")
print("Original Series:")
print(s)
try:
# Adding an existing category
s = s.cat.add_categories(["apple"])
except ValueError as e:
print("\nError encountered:", e)
Following is an output of the above code −
Original Series:
0 apple
1 banana
2 cherry
dtype: category
Categories (3, object): ['apple', 'banana', 'cherry']
Error encountered: new categories must not include old categories: {'apple'}