Python Pandas - Append a collection of Index options together

PythonServer Side ProgrammingProgramming

To append a collection of Index options together, use the append() method in Pandas. At first, import the required libraries −

import pandas as pd

Creating Pandas index −

index1 = pd.Index([10, 20, 30, 40, 50])

Display the Pandas index −

print("Pandas Index...\n",index1)

Create a new index to be appended −

index2 = pd.Index([60, 70, 80])

Append the new index −

print("\nAfter appending...\n",index1.append(index2))

Example

Following is the code −

import pandas as pd

# Creating Pandas index
index1 = pd.Index([10, 20, 30, 40, 50])

# Display the Pandas index
print("Pandas Index...\n",index1)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index1.size)

# create a new index to be appended
index2 = pd.Index([60, 70, 80])

# Append the new index
print("\nAfter appending...\n",index1.append(index2))

Output

This will produce the following output −

Pandas Index...
Int64Index([10, 20, 30, 40, 50], dtype='int64')

Number of elements in the index...
5

After appending...
Int64Index([10, 20, 30, 40, 50, 60, 70, 80], dtype='int64')
raja
Published on 14-Oct-2021 06:48:32
Advertisements