concat two category type series in pandas

Concat Category Type Series in Pandas

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?

concat two category type series in pandas

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top