Pandas Tutorial Pandas References

Pandas Series - append() function



The Pandas Series append() function concatenates two or more Series.

Syntax

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

Parameters

to_append Required. Specify data to append with self as another Series or list/tuple of Series.
ignore_index Optional. If set to True, the resulting axis will be labeled 0, 1, …, n - 1. Default: False.
verify_integrity Optional. If set to True, raise ValueError on creating index with duplicates. Default: False.

Return Value

Returns concatenated Series.

Example: append() example

In the example below, the append() function is used to concatenate series.

import pandas as pd
import numpy as np

x1 = pd.Series([1, 2, 3])
x2 = pd.Series([10, 20, 30])
x3 = pd.Series([11, 12, 13], index=['a', 'b', 'c'])

print("x1.append(x2) returns:")
print(x1.append(x2),"\n")

print("x1.append(x3) returns:")
print(x1.append(x3),"\n")

The output of the above code will be:

x1.append(x2) returns:
0     1
1     2
2     3
0    10
1    20
2    30
dtype: int64 

x1.append(x3) returns:
0     1
1     2
2     3
a    11
b    12
c    13
dtype: int64 

Example: using ignore_index parameter

When ignore_index is set to True, the resulting axis will be labeled 0, 1, …, n - 1. Consider the example below:

import pandas as pd
import numpy as np

x1 = pd.Series([1, 2, 3])
x2 = pd.Series([11, 12, 13], index=['a', 'b', 'c'])

print("x1.append(x2) returns:")
print(x1.append(x2),"\n")

print("x1.append(x2, ignore_index=True) returns:")
print(x1.append(x2, ignore_index=True),"\n")

The output of the above code will be:

x1.append(x2) returns:
0     1
1     2
2     3
a    11
b    12
c    13
dtype: int64 

x1.append(x2, ignore_index=True) returns:
0     1
1     2
2     3
3    11
4    12
5    13
dtype: int64 

❮ Pandas Series - Functions

5