Skip to Content

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.

# 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. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.