Signup/Sign In
PUBLISHED ON: MARCH 31, 2021

Pandas Series append() Method

If we want to merge two Series we can use the Python pandas Series.append() method, This method merges or concatenate two Series and returns a new Series.

The below is the syntax of the Series.append() method.

Syntax

Series.append(to_append, ignore_index=False, verify_integrity=False)

Parameters

to_append: It can be a Series or list/tuple of Series. It indicates the Series to append with self.

ignore_index: It represents the bool(True or False), and the default False. If this parameter is True, the resulting axis will be labeled as 0, 1, …, n - 1.

verify_integrity: It represents the bool(True or False), and the default False. If this parameter is True, it raises an Exception on creating an index with duplicates.

Example: Append two Series in Pandas

We can append a Series to a Series using Series.append() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
s1 = pd.Series(['Python','Java'])
s2 = pd.Series([1,2])
print(s1.append(s2))


0 Python
1 Java
0 1
1 2
dtype: object

Example 2: Append two Series in Pandas

In the previous example, when we append two Series, their index values are overlapped. We can avoid this by passing the parameter ignore_index=True in the Series.append() method. See the below example. Now the index will start from 0 to n-1.

#importing pandas as pd
import pandas as pd
#creating Series
s1 = pd.Series(['Python','Java'])
s2 = pd.Series([1,2])
print(s1.append(s2, ignore_index=True))


0 Python
1 Java
2 1
3 2
dtype: object

Example 3: Append two Series in Pandas

This will throw exceptions when the index value overlaps. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
s1 = pd.Series(['Python','Java'])
s2 = pd.Series([1,2])
print(s1.append(s2,verify_integrity=True))


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

Conclusion

In this tutorial, we learned the python pandas DataFrame.append() method. We understood the syntax, parameters of the DataFrame.append() method and solved examples by applying this method to the Series.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.