Python | Pandas Series.append()

27 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.append() function is used to concatenate two or more series object.
Syntax: Series.append(to_append, ignore_index=False, verify_integrity=False)
Parameter :to_append : Series or list/tuple of Seriesignore_index : If True, do not use the index labels.verify_integrity : If True, raise Exception on creating index with duplicates
Returns : appended : Series
Example #1: Use Series.append() function to append the passed series object at the end of this series object.
# importing pandas as pdimport pandas as pd  # Creating the first Seriessr1 = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio'])  # Create the first Indexindex_1 = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']   # set the index of first seriessr1.index = index_1  # Creating the second Seriessr2 = pd.Series(['Chicage', 'Shanghai', 'Beijing', 'Jakarta', 'Seoul'])  # Create the second Indexindex_2 = ['City 6', 'City 7', 'City 8', 'City 9', 'City 10']   # set the index of second seriessr2.index = index_2  # Print the first seriesprint(sr1)  # Print the second seriesprint(sr2)
Output :
City 1    New York
City 2     Chicago
City 3     Toronto
City 4      Lisbon
City 5         Rio
dtype: object

City 6      Chicage
City 7     Shanghai
City 8      Beijing
City 9      Jakarta
City 10       Seoul
dtype: object
Now we will use Series.append() function to append sr2 at the end of sr1 series.
# append sr2 at the end of sr1result = sr1.append(sr2)  # Print the resultprint(result)
Output :
City 1     New York
City 2      Chicago
City 3      Toronto
City 4       Lisbon
City 5          Rio
City 6      Chicage
City 7     Shanghai
City 8      Beijing
City 9      Jakarta
City 10       Seoul
dtype: object
As we can see in the output, the Series.append() function has successfully append the sr2 object at the end of sr1 object. Example #2: Use Series.append() function to append the passed series object at the end of this series object. Ignore the original index of the two series objects.
# importing pandas as pdimport pandas as pd  # Creating the first Seriessr1 = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio'])  # Create the first Indexindex_1 = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']   # set the index of first seriessr1.index = index_1  # Creating the second Seriessr2 = pd.Series(['Chicage', 'Shanghai', 'Beijing', 'Jakarta', 'Seoul'])  # Create the second Indexindex_2 = ['City 6', 'City 7', 'City 8', 'City 9', 'City 10']   # set the index of second seriessr2.index = index_2  # Print the first seriesprint(sr1)  # Print the second seriesprint(sr2)
Output :
City 1    New York
City 2     Chicago
City 3     Toronto
City 4      Lisbon
City 5         Rio
dtype: object

City 6      Chicage
City 7     Shanghai
City 8      Beijing
City 9      Jakarta
City 10       Seoul
dtype: object
Now we will use Series.append() function to append sr2 at the end of sr1 series. We are going to ignore the index of the given series object.
# append sr2 at the end of sr1# ignore the indexresult = sr1.append(sr2, ignore_index = True)  # Print the resultprint(result)
Output :
0    New York
1     Chicago
2     Toronto
3      Lisbon
4         Rio
5     Chicage
6    Shanghai
7     Beijing
8     Jakarta
9       Seoul
dtype: object
As we can see in the output, the Series.append() function has successfully append the sr2 object at the end of sr1 object and it has also ignored the index.
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.