In this tutorial, we will look at how to concat two category type Pandas series with the help of some examples.
How to combine category type Pandas series?
You can use the Pandas concat()
function to combine two category type Pandas series. The following is the syntax –
# concat pandas series pd.concat([s1, s2])
Combining series with the same categories results in a category type series. In other cases, the resulting type will depend on the underlying categories.
Examples
Let’s look at some examples of combining two category type series in Pandas.
Concat category type Pandas series with the same categories
First, let’s combine two categorical Pandas series having the same category values using the pd.concat()
function.
import pandas as pd # create category type pandas series s1 = pd.Series(['a', 'b']).astype('category') s2 = pd.Series(['a', 'b', 'b']).astype('category') # combine the series print(pd.concat([s1, s2]))
Output:
0 a 1 b 0 a 1 b 2 b dtype: category Categories (2, object): ['a', 'b']
The resulting combined series is of category type with the same categories.
Concat category type Pandas series with different categories
Let’s now create two category type Pandas series having different categories.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# create category type pandas series s1 = pd.Series(['a', 'b']).astype('category') s2 = pd.Series(['a', 'b', 'c']).astype('category') # display the series print(s1) print(s2)
Output:
0 a 1 b dtype: category Categories (2, object): ['a', 'b'] 0 a 1 b 2 c dtype: category Categories (3, object): ['a', 'b', 'c']
You can see that the series s1
has the categories “a” and “b” whereas the series s2
has the categories “a”, “b”, and “c”.
Let’s now combine them with the pd.concat()
function and see the outcome.
# combine the series print(pd.concat([s1, s2]))
Output:
0 a 1 b 0 a 1 b 2 c dtype: object
You can see that the resulting series is of object
type.
If you want to combine Pandas series with different categories into a category type series, use the union_categoricals()
function.
For example, let’s combine the above two series using the union_categoricals()
function.
from pandas.api.types import union_categoricals # combine series with different categories print(union_categoricals([s1, s2]))
Output:
['a', 'b', 'a', 'b', 'c'] Categories (3, object): ['a', 'b', 'c']
Note that the resulting categorical has unique categories combined from both the series.
For more on the union_categoricals()
function in Pandas, refer to this tutorial.
You might also be interested in –
- Concat DataFrames in Pandas
- Pandas Category Column to a List
- Pandas – Set Category Order of a Categorical Column
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.