Two pandas series appended to one

Append Two Pandas Series

You can apply a number of different operations and manipulations on pandas series, one of which is to append different series together. In this tutorial, we will look at how to append one pandas series to another.

To append a pandas series, you can use the pandas series append() function. You can pass the series you want to append as an argument to the function. The following is the syntax:

# using pandas series append()
s3 = s1.append(s2)

Here, s1 is the series you want to append the series s2 to. The append() function returns an appended series. Note that the returned series retains the index from both the original series (see the examples below).

Let’s look at some examples of appending pandas series. We’ll also look at the usage of parameters ignore_index and verify_integrity to the append() function.

First, let’s create two pandas series s1 and s2. We’ll be appending the series s2 to s1 using the append() function.

import pandas as pd

# create pandas series
s1 = pd.Series(['a','b','c'])
s2 = pd.Series(['d','e','f'])

# append the series
s3 = s1.append(s2)
# display the appended series
print(s3)

Output:

0    a
1    b
2    c
0    d
1    e
2    f
dtype: object

You can see that the returned series s3 has values of both s1 and s2 appended together. Notice that s3 retains the original index of the individual series s1 and s2.

If you want the appended series to have a new index, you can pass ignore_index=True to the append() function.

📚 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.

# append the series
s3 = s1.append(s2, ignore_index=True)
# display the appended series
print(s3)

Output:

0    a
1    b
2    c
3    d
4    e
5    f
dtype: object

The returned series from the append() function now does not retain the original indexes.

Conversely, if you want to append two series only if there are no duplicate indexes you can pass verify_integrity=True to the append function.

# create pandas series
s1 = pd.Series(['a','b','c'])
s2 = pd.Series(['d','e','f'])

# append the series
s3 = s1.append(s2, verify_integrity=True)
# display the appended series
print(s3)

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-ef3bd8f0ea66> in <module>
      4 
      5 # append the series
----> 6 s3 = s1.append(s2, verify_integrity=True)
      7 # display the appended series
      8 print(s3)

** Some lines have been skipped to keep the error message short **

~\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in _maybe_check_integrity(self, concat_index)
    578             if not concat_index.is_unique:
    579                 overlap = concat_index[concat_index.duplicated()].unique()
--> 580                 raise ValueError(
    581                     "Indexes have overlapping values: "
    582                     "{overlap!s}".format(overlap=overlap)

ValueError: Indexes have overlapping values: Int64Index([0, 1, 2], dtype='int64')

The above code resulted in an error because the series s1 and s2 had overlapping indexes. If you change the index of let’s say s2 so that there are no duplicate indexes and run the code again:

# create pandas series
s1 = pd.Series(['a','b','c'])
s2 = pd.Series(['d','e','f'], index=[3,4,5])

# append the series
s3 = s1.append(s2, verify_integrity=True)
# display the appended series
print(s3)

Output:

0    a
1    b
2    c
3    d
4    e
5    f
dtype: object

We get the appended series since both s1 and s2 did not contain overlapping indexes when keeping verify_integrity=True.

For more on the pandas series append() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


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


Tutorials on pandas series –

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