w3resource

Pandas Series: append() function

Concatenate two or more Pandas series

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

Syntax:

Series.append(self, to_append, ignore_index=False, verify_integrity=False)
Pandas Series append image
Name Description Type/Default Value Required / Optional
to_append   Series to append with self. Series or list/tuple of Series Required
ignore_index If True, do not use the index labels. bool
Default Value: False
Required
verify_integrity If True, raise Exception on creating index with duplicates. bool
Default Value: False
Required

Returns: Series - Concatenated Series.

Notes:

Iteratively appending to a Series can be more computationally intensive than a single concatenate. A better solution is to append values to a list and then concatenate the list with the original Series all at once.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series([2, 3, 4])
s2 = pd.Series([5, 6, 7])
s3 = pd.Series([5, 6, 7], index=[4, 5, 6])
s1.append(s2)

Output:

0    2
1    3
2    4
0    5
1    6
2    7
dtype: int64
Pandas Series append image

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series([2, 3, 4])
s2 = pd.Series([5, 6, 7])
s3 = pd.Series([5, 6, 7], index=[4, 5, 6])
s1.append(s3)

Output:

0    2
1    3
2    4
4    5
5    6
6    7
dtype: int64

Example - With ignore_index set to True:

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series([2, 3, 4])
s2 = pd.Series([5, 6, 7])
s3 = pd.Series([5, 6, 7], index=[4, 5, 6])
s1.append(s2, ignore_index=True)

Output:

0    2
1    3
2    4
3    5
4    6
5    7
dtype: int64

With verify_integrity set to True:

s1.append(s2, verify_integrity=True) Traceback (most recent call last): ... ValueError: Indexes have overlapping values: [0, 1, 2]

Previous: Create a new view of Pandas Series
Next: Replace Pandas series values given in to_replace with value



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-append.php