Pandas Series.append()
function is used to append two or more series objects. It takes the Series(which is appended to another Series) as an argument and returns appended
Pandas Series.
Series is a One-dimensional ndarray with axis labels. The labels must be a hashable type. It supports both integer and labeled indexing. The row labels of the Series are called the index. It can be stored in any of the data types(integer, string, float, python objects, etc.) The Series can have only one column. So that, we can easily convert Series to list, Series to NumPy Array, and Series to Python Dictionary.
In this article, I will explain how to append one Pandas series to another Series using Pandas Series.append() function.
1. Quick Examples of Append Pandas Series
Following are quick examples of how to append multiple series.
# Below are the quick examples
# Example 1: Append two Series using append()
append_ser = ser1.append(ser2)
# Example 2: Append two series set ignore_index = True
append_ser = ser1.append(ser2, ignore_index = True)
# Example 3: Append two series set verify_integrity = True
# having duplicate indexes
append_ser = ser1.append(ser2, verify_integrity = True)
# Example 4: Append two Series set verify_integrity = True
# Having no duplicate indexes
# Customize ser2 index
ser2 = pd.Series(['Spark', 'PySpark', 'Pandas'], index = ['a', 'b', 'c'])
append_ser = ser1.append(ser2, verify_integrity = True)
# Example 5: Append Series as a row of DataFrame
append_ser = df.append(ser, ignore_index=True)
2. Syntax of Series.append()
Following is the syntax of Series.append() function.
# Syntax of the Series.append()
Series.append(to_append, ignore_index=False, verify_integrity=False)
2.1 Parameters of the Series.append()
Following are the parameters of the append() function.
to_append :
Series or list/tuple of Seriesignore_index :
(Default False)If it is True, ignores given Series indexes.verify_integrity :
(Default False)If it is True, raises an Exception for creating duplicate indexes.
2.2 Return Value
It returns appended Series.
3. Append Pandas Series
In Pandas append()
function is used to concat two or more series and return the appended Series where, the indexes are returned from both original Series. If you want to ignore the indexes of the given Series, you can set ignore_index = True
, then it returns non-duplicated indexes.
Let’s create two Pandas Series,
import pandas as pd
# Create two Pandas Series from list
ser1 = pd.Series(['python', 'php', 'java'])
print(ser1)
ser2 = pd.Series(['Spark', 'PySpark', 'Pandas'])
print(ser2)
# Output:
# 0 python
# 1 php
# 2 java
# dtype: object
# 0 Spark
# 1 PySpark
# 2 Pandas
# dtype: object
Let’s take these two Series and apply append() function, it will return the appended Series where the elements are taken from both given Series.
# Append two Series using append()
append_ser = ser1.append(ser2)
print(append_ser)
# Output:
# 0 python
# 1 php
# 2 java
# 0 Spark
# 1 PySpark
# 2 Pandas
# dtype: object
4. Set ignore_index = True Append Pandas Series
When we want to return appended series without duplicate index, we can pass ignore_index = True
to the append()
function. It returns appended series without original indexes. By using this param it assigns the new index with a value starting from 0 and increments by 1 for each row.
# Append two series set ignore_index = True
append_ser = ser1.append(ser2, ignore_index = True)
print(append_ser)
# Output:
# 0 python
# 1 php
# 2 java
# 3 Spark
# 4 PySpark
# 5 Pandas
# dtype: object
5. Set verify_integrity=True
If you want to fail the append two pandas series when both Series have the same indexes use param verify_integrity=True
into the append() function.
Let’s pass & check verify_integrity=True
to the append function when both Series have duplicate indexes.
# Append two series set verify_integrity = True
append_ser = ser1.append(ser2, verify_integrity = True)
print(append_ser)
# Output:
# ValueError: Indexes have overlapping values
As you can see from the above code, it returned ValueError
.
Now, customize one of the given Series indexes, then apply append() function on given Series along with param verify_integrity = True.
# Customize ser2 index
ser2 = pd.Series(['Spark', 'PySpark', 'Pandas'], index = ['a', 'b', 'c'])
print(ser2)
# Append two Series set verify_integrity = True
append_ser = ser1.append(ser2, verify_integrity = True)
print(append_ser)
# Output:
# 0 python
# 1 php
# 2 java
# a Spark
# b PySpark
# c Pandas
# dtype: object
It has returned appended Series without duplicate indexes.
7. Append Pandas Series as a Row of DataFrame using append()
Now, let’s create a DataFrame with a few rows and columns, Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# Create a pandas DataFrame.
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee' :[22000,25000,23000,24000,2600],
'Duration':['30days','50days','35days','40days','60days'],
'Discount':[1000,2300,1000,1200,2500]
})
df = pd.DataFrame(technologies, columns=['Courses', 'Fee', 'Duration', 'Discount'])
print(df)
# Output:
# Courses Fee Duration Discount
# 0 Spark 22000 30days 1000
# 1 PySpark 25000 50days 2300
# 2 Hadoop 23000 35days 1000
# 3 Python 24000 40days 1200
# 4 Pandas 2600 60days 2500
Let’s create a Series with customized indexes and then, apply the append() function. Here, I want to append a Series as a row of DataFrame. Let’s apply,
# Create Pandas Series
ser = pd.Series(['Java', '20000', '50days', 1500], index = ['Courses', 'Fee', 'Duration', 'Discount'])
print(ser)
# Append Series as a row of DataFrame
append_ser = df.append(ser, ignore_index=True)
print(append_ser)
Yields below output.
# Output:
Courses Fee Duration Discount
0 Spark 22000 30days 1000
1 PySpark 25000 50days 2300
2 Hadoop 23000 35days 1000
3 Python 24000 40days 1200
4 Pandas 2600 60days 2500
5 Java 20000 50days 1500
As you can see, the given Series has been appended to the given DataFrame where the index of the Series became the column labels of the DataFrame.
8. Conclusion
In this article, I have explained how to append one series to another Series using the append() function and its syntax and parameters with several examples and also I explained how to add a Series as a row of Pandas DataFrame using the append() function.