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.
How to append a pandas series?
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).
Examples
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.
Appending two pandas series
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.
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.
# 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 –
- Convert Pandas Series to a DataFrame
- Convert Pandas Series to a List
- Convert Pandas Series to a NumPy Array
- Convert Pandas Series to a Dictionary
- Sort a Pandas Series
- Append Two Pandas Series
- Apply a Function to a Pandas Series
- Pandas – Shift column values up or down
- Plot a Histogram of Pandas Series Values
- Create a Pie Chart of Pandas Series Values
- Plot a Bar Chart of Pandas Series Values
- Create a Boxplot from Pandas Series Values
- Create a Density Plot from Pandas Series Values