Index (Pandas)
A pandas Index is the immutable array of row or column labels attached to a Series or DataFrame. It powers fast lookups, automatic data alignment, and meaningful row identifiers beyond simple integer positions.
Pandas
Every Series and DataFrame has an Index for its rows; a DataFrame also has one for its columns. A default RangeIndex uses consecutive integer labels. set_index can use existing columns as row labels; labels still identify records rather than becoming positions.
Index objects reject element assignment, but the Index itself is not hashable. Its individual labels must be hashable. To change labels, construct or assign a replacement index; immutability does not imply that hash(index) succeeds.
Duplicate labels are allowed. A scalar loc lookup can then select several rows, so check index.is_unique when an operation requires one record per label. reset_index normally retains the old labels in a column; use drop=True only when discarding them is intended.
Syntax
pd.Index(data, dtype=None, name=None)Examples
Python
import pandas as pd
index = pd.Index(["a", "a", "b"])
print(index.tolist())
print(index.is_unique)
try:
index[0] = "c"
except TypeError as error:
print(type(error).__name__ + ": " + str(error))
try:
hash(index)
except TypeError as error:
print(type(error).__name__ + ": " + str(error))Output:
['a', 'a', 'b']
False
TypeError: Index does not support mutable operations
TypeError: unhashable type: 'Index'