Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Append a collection of Index options together
To append a collection of Index options together, use the append() method in Pandas. This method combines multiple Index objects into a single Index without modifying the original indexes.
Creating Basic Pandas Index
First, let's create a basic Pandas Index ?
import pandas as pd
# Creating Pandas index
index1 = pd.Index([10, 20, 30, 40, 50])
# Display the Pandas index
print("Pandas Index...\n", index1)
print("\nNumber of elements in the index...\n", index1.size)
Pandas Index... Index([10, 20, 30, 40, 50], dtype='int64') Number of elements in the index... 5
Appending Single Index
Create a second index and append it to the first one ?
import pandas as pd
index1 = pd.Index([10, 20, 30, 40, 50])
index2 = pd.Index([60, 70, 80])
# Append the new index
result = index1.append(index2)
print("After appending...\n", result)
After appending... Index([10, 20, 30, 40, 50, 60, 70, 80], dtype='int64')
Appending Multiple Indexes
You can append multiple indexes at once by passing a list ?
import pandas as pd
index1 = pd.Index([10, 20, 30])
index2 = pd.Index([40, 50])
index3 = pd.Index([60, 70, 80])
# Append multiple indexes
result = index1.append([index2, index3])
print("After appending multiple indexes...\n", result)
After appending multiple indexes... Index([10, 20, 30, 40, 50, 60, 70, 80], dtype='int64')
Key Points
- The
append()method returns a new Index object - Original indexes remain unchanged
- You can append single or multiple indexes
- All indexes must have compatible data types
Conclusion
Use append() to combine multiple Pandas Index objects. The method preserves the original indexes and returns a new combined Index with all elements in order.
Advertisements
