• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:18 mins read
You are currently viewing How to Append Pandas Series?

Pandas Series.append() function is used to append two or more series objects. It takes the Series(which is appended to another Series) as an argument and returns appended Pandas Series.

Series is a One-dimensional ndarray with axis labels. The labels must be a hashable type. It supports both integer and labeled indexing. The row labels of the Series are called the index. It can be stored in any of the data types(integer, string, float, python objects, etc.) The Series can have only one column. So that, we can easily convert Series to list, Series to NumPy Array, and Series to Python Dictionary.

In this article, I will explain how to append one Pandas series to another Series using the Pandas Series.append() function.

Key Points –

  • The append() method in Pandas allows for the combination of two or more Series into a single Series, facilitating data aggregation or extension.
  • Invoke append() on the Series you wish to extend, providing the Series or data to append as an argument.
  • Consider optional parameters such as ignore_index to reset the index of the appended data and verify_integrity to check for duplicate indices.
  • Decide whether to modify the original Series by setting the inplace=True parameter.
  • Ensure that the data types and structures of the Series being appended are compatible to avoid errors or unexpected outcomes.
  • Append operations can be performed iteratively for sequential addition of Series or concurrently by passing a list of Series, providing flexibility in data manipulation.

1. Quick Examples of Append Pandas Series

Following are quick examples of how to append multiple series.


# Below are the quick examples

# Example 1: Append two Series using append()
append_ser = ser1.append(ser2)

# Example 2: Append two series set ignore_index = True 
append_ser = ser1.append(ser2, ignore_index = True)

# Example 3: Append two series set verify_integrity = True
# having duplicate indexes
append_ser = ser1.append(ser2, verify_integrity = True)

# Example 4: Append two Series set verify_integrity = True
# Having no duplicate indexes
# Customize ser2 index
ser2 = pd.Series(['Spark', 'PySpark', 'Pandas'], index = ['a', 'b', 'c'])
append_ser = ser1.append(ser2, verify_integrity = True)

# Example 5: Append Series as a row of DataFrame
append_ser = df.append(ser, ignore_index=True)

2. Syntax of Series.append()

Following is the syntax of Series.append() function.


# Syntax of the Series.append()
Series.append(to_append, ignore_index=False, verify_integrity=False)

2.1 Parameters of the Series.append()

Following are the parameters of the append() function.

to_append – This parameter represents the data to be appended to the Series. It can be another Series, DataFrame, scalar value, or list-like structure containing data.
ignore_index – A boolean parameter indicating whether to reset the index of the appended data. If set to True, the resulting Series will have a new index generated automatically. The default value is False.
verify_integrity – A boolean parameter indicating whether to check for duplicate indices in the appended data. If set to True, it will raise a ValueError if duplicate indices are found. The default value is False.

2.2 Return Value

It returns an appended Series.

3. Append Pandas Series

In Pandas append() function is used to concat two or more series and return the appended Series where the indexes are returned from both original Series. If you want to ignore the indexes of the given Series, you can set ignore_index = True, and then it returns non-duplicated indexes.

Let’s create two Pandas Series,


import pandas as pd
# Create two Pandas Series from list
ser1 = pd.Series(['python', 'php', 'java'])
print("First Series:\n", ser1)
ser2 = pd.Series(['Spark', 'PySpark', 'Pandas'])
print("Second Series:\n", ser2)

Yields below output.

pandas series append

Let’s take these two Series and apply the append() function, it will return the appended Series where the elements are taken from both given Series.


# Append two Series using append()
append_ser = ser1.append(ser2)
print("After appending the Series:\n", append_ser)
pandas series append

4. Set ignore_index = True Append Pandas Series

When we want to return the appended series without a duplicate index, we can pass ignore_index=True to the append() function. It returns appended series without original indexes. By using this param it assigns the new index with a value starting from 0 and increments by 1 for each row.


# Append two series set ignore_index = True 
append_ser = ser1.append(ser2, ignore_index = True)
print(append_ser)

# Output:
# 0     python
# 1        php
# 2       java
# 3      Spark
# 4    PySpark
# 5     Pandas
# dtype: object

5. Set verify_integrity=True

If you want to fail the append two pandas series when both Series have the same indexes use the param verify_integrity=True into the append() function.

Let’s pass & check verify_integrity=True to the append function when both Series have duplicate indexes.


# Append two series set verify_integrity = True
append_ser = ser1.append(ser2, verify_integrity = True)
print(append_ser)

# Output:
# ValueError: Indexes have overlapping values

As you can see from the above code, it returned ValueError.

Now, customize one of the given Series indexes, then apply append() function on given Series along with param verify_integrity=True.


# Customize ser2 index
ser2 = pd.Series(['Spark', 'PySpark', 'Pandas'], index = ['a', 'b', 'c'])
print(ser2)
# Append two Series set verify_integrity = True 
append_ser = ser1.append(ser2, verify_integrity = True)
print(append_ser)

# Output:
# 0     python
# 1        php
# 2       java
# a      Spark
# b    PySpark
# c     Pandas
# dtype: object

It has returned the appended Series without duplicate indexes.

6. Append Pandas Series as a Row of DataFrame using append()

Now, let’s create a DataFrame with a few rows and columns, Our DataFrame contains column names CoursesFeeDuration, and Discount.


# Create a pandas DataFrame.
import pandas as pd
technologies   = ({
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
    'Fee' :[22000,25000,23000,24000,2600],
    'Duration':['30days','50days','35days','40days','60days'],
    'Discount':[1000,2300,1000,1200,2500]
                })
df = pd.DataFrame(technologies, columns=['Courses', 'Fee', 'Duration', 'Discount'])
print(df)

# Output:
#   Courses    Fee Duration  Discount
# 0    Spark  22000   30days      1000
# 1  PySpark  25000   50days      2300
# 2   Hadoop  23000   35days      1000
# 3   Python  24000   40days      1200
# 4   Pandas   2600   60days      2500

Let’s create a Series with customized indexes and then, apply the append() function. Here, I want to append a Series as a row of DataFrame. Let’s apply,


# Create Pandas Series
ser = pd.Series(['Java', '20000', '50days', 1500], index = ['Courses', 'Fee', 'Duration', 'Discount'])
print(ser)
# Append Series as a row of DataFrame
append_ser = df.append(ser, ignore_index=True)
print(append_ser)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
0    Spark  22000   30days      1000
1  PySpark  25000   50days      2300
2   Hadoop  23000   35days      1000
3   Python  24000   40days      1200
4   Pandas   2600   60days      2500
5     Java  20000   50days      1500

As you can see, the given Series has been appended to the given DataFrame where the index of the Series became the column labels of the DataFrame.

Frequently Asked Questions on How to Append Pandas Series

What is the purpose of appending Pandas Series?

Appending Pandas Series allows you to combine data from multiple Series into a single Series, which can be useful for concatenating datasets or adding new observations.

How can I append one Series to another in Pandas?

You can use the append() method available for Pandas Series. Simply call append() on the Series you want to extend and provide the Series or data you want to append as an argument.

Can I append multiple Series at once?

You cannot directly append multiple Series at once using a single append() call in pandas. The append() method is designed to append one Series or data at a time to another Series.

What should I be cautious about when appending Series?

Ensure that the data types and structures of the Series being appended are compatible to avoid errors or unexpected outcomes. Additionally, be mindful of duplicate indices when appending Series, especially if the verify_integrity parameter is not set to True.

Are there any performance considerations when appending Series?

Appending Series involves creating a new Series object, which can be resource-intensive for large datasets. Consider alternatives such as concatenating DataFrames if you’re working with substantial amounts of data to optimize performance.

Does appending Series modify the original Series?

By default, appending Series returns a new Series object without modifying the original Series. However, you can choose to modify the original Series in place by setting the inplace=True parameter.

Conclusion

In this article, I have explained how to append one series to another Series using the append() function and its syntax and parameters with several examples and also I explained how to add a Series as a row of Pandas DataFrame using the append() function.

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium