Pandas Series Append Method

PythonPythonBeginner
Practice Now

Introduction

The pandas Series.append() method is used to merge or concatenate two Series and return a new Series. It allows you to combine the elements of two Series together, either with or without preserving the original index values.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") subgraph Lab Skills pandas/select_columns -.-> lab-68732{{"`Pandas Series Append Method`"}} python/booleans -.-> lab-68732{{"`Pandas Series Append Method`"}} python/lists -.-> lab-68732{{"`Pandas Series Append Method`"}} python/tuples -.-> lab-68732{{"`Pandas Series Append Method`"}} python/importing_modules -.-> lab-68732{{"`Pandas Series Append Method`"}} python/numerical_computing -.-> lab-68732{{"`Pandas Series Append Method`"}} python/data_analysis -.-> lab-68732{{"`Pandas Series Append Method`"}} end

Import the pandas module

To use the Series.append() method, we first need to import the pandas module in Python. You can do this by using the following code:

import pandas as pd

Create two Series

Next, we need to create two Series that we want to append together. You can create a Series by passing a list or array-like object to the pd.Series() constructor. For example:

s1 = pd.Series(['Python', 'Java'])
s2 = pd.Series([1, 2])

Append the Series

Now, we can use the Series.append() method to append s2 to s1. You can do this by calling the append() method on s1 and passing s2 as the to_append parameter. For example:

s3 = s1.append(s2)

(Optional) Ignore index values

By default, the append() method preserves the index values of the original Series. If you want to ignore the index values and generate a new index from 0 to n-1, you can pass the ignore_index=True parameter to the append() method. For example:

s3 = s1.append(s2, ignore_index=True)

(Optional) Verify integrity

If you want to ensure that the resulting index does not have any duplicate values, you can pass the verify_integrity=True parameter to the append() method. If there are any overlapping index values, it will raise a ValueError. For example:

s3 = s1.append(s2, verify_integrity=True)

Summary

In this lab, we learned how to use the Series.append() method in pandas to merge or concatenate two Series. We walked through the steps of importing the pandas module, creating the Series, and appending them together. Additionally, we explored the optional parameters to ignore index values or verify integrity. This method is useful when we want to combine two Series into a single Series in pandas.

Other Python Tutorials you may like