Series append – Python Pandas Series append() Function

Pandas Series append() Function in Python:

Series append: The append() function of Pandas Series is used to concatenate two or more Series.

Syntax:

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

Parameters

to_append: This is required. It indicates the data to append with self. The type of this may be Series or list/tuple of Series.

ignore_index: This is optional. The resulting axis will be labelled 0, 1,…, n – 1 if True is set. False is the default value.

verify_integrity: This is optional. If set to true, it raises a ValueError when an index is created with duplicates. False is the default value.

Return Value:

The concatenated series is returned by the append() function of Pandas Series

Pandas Series append() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Pass some random list as an argument to the Series() function of the pandas module to create another series.
  • Store it in another variable.
  • Similarly, create another series by passing the index values and store it in another variable.
  • Concatenate/ Append the given second series with the first series using the append() function and print the result.
  • Append the given third series with the second series using the append() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series1 = pd.Series([5, 6, 7, 8])
# Pass some random list as an argument to the Series() function
# of the pandas module to create another series.
# Store it in another variable. 
gvn_series2 = pd.Series([25, 35, 45, 55])
# Similarly create another series by passing the index values and 
# store it in another variable. 
gvn_series3 = pd.Series([50, 100, 150, 200], index=['p', 'q', 'r', 's'])
# Concatenate/ Append the given second series with the first series using the 
# append() function and print the result.
print("Concatenating the given second series with the first series:")
print(gvn_series1.append(gvn_series2))
print()
# Append the given third series with the second series using the 
# append() function and print the result.
print("Concatenating the given third series with the second series:")
print(gvn_series2.append(gvn_series3))

Output:

Concatenating the given second series with the first series:
0     5
1     6
2     7
3     8
0    25
1    35
2    45
3    55
dtype: int64

Concatenating the given third series with the second series:
0     25
1     35
2     45
3     55
p     50
q    100
r    150
s    200
dtype: int64

Example2: Ignoring index values

Pandas series append: When ignore index is set to True, the axis labels will be 0, 1,…, n – 1. Here index values are ignored while concatenating/appending.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Similarly, create another series by passing the index values and store it in another variable.
  • Concatenate/ Append the given second series with the first series using the append() function and print the result.
  • Concatenate/ Append the given second series with the first series using the append() function with ignoring the index values using the ignore_index argument set to True and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series1 = pd.Series([5, 6, 7, 8])
# Similary create another series by passing the index values and 
# store it in another variable. 
gvn_series2 = pd.Series([50, 100, 150, 200], index=['p', 'q', 'r', 's'])
# Concatenate/ Append the given second series with the first series using the 
# append() function and print the result.
print("Concatenating the given second series with the first series:")
print(gvn_series1.append(gvn_series2))
print()
# Concatenate/ Append the given second series with the first series using the 
# append() function with ignoring the index values using the ignore_index
# argument set to True and print the result.
print("Concatenating the given second series with the first ignoring index values:")
print(gvn_series1.append(gvn_series2, ignore_index=True))

Output:

Concatenating the given second series with the first series:
0      5
1      6
2      7
3      8
p     50
q    100
r    150
s    200
dtype: int64

Concatenating the given second series with the first ignoring index values:
0      5
1      6
2      7
3      8
4     50
5    100
6    150
7    200
dtype: int64