Label rows
Use an Index to assign meaningful row labels to a DataFrame and select records with .loc.
Pandas · Class
pandas.Index is an immutable sequence of labels used for indexing and alignment. It is the core axis-label object used by pandas Series and DataFrame objects.
pandas.Index creates an immutable, one-dimensional sequence of axis labels. Construct it from array-like data, optionally controlling its dtype, name, copying behavior, and whether tuple-like data should become a MultiIndex.
class pandas.Index(*data=None*, *dtype=None*, *copy=None*, *name=None*, *tupleize_cols=True*)| Name | Type | Description |
|---|---|---|
data | array-like, optional | One-dimensional array-like data for the labels, such as a list, NumPy array, or pandas Series. |
dtype | str, numpy.dtype, or ExtensionDtype, optional | Requested output data type; if omitted, pandas infers it from data. |
copy | bool, default False | Whether to copy the input data. |
name | object, optional | Name stored on the Index. |
tupleize_cols | bool, default True | Whether to attempt MultiIndex construction from suitable tuple-like data. |
A pandas.Index object containing the supplied labels.
import pandas as pd
labels = pd.Index(["a", "b", "c"], name="label")
frame = pd.DataFrame({"value": [10, 20, 30]}, index=labels)
print(frame.loc["b", "value"])
print(frame.index.name)Creates a named Index and uses it as a DataFrame axis for label-based selection.
Use an Index to assign meaningful row labels to a DataFrame and select records with .loc.
Use named Index objects to make axis meaning explicit in downstream transformations.
Construct an Index from existing one-dimensional data before alignment or comparison operations.
pandas.Index is an immutable sequence used for indexing and alignment, and it stores axis labels for pandas objects. It can be constructed from one-dimensional array-like input such as a list, NumPy array, or Series.
Follow-up: How does tupleize_cols affect construction? When true, pandas attempts to create a MultiIndex when the input makes that possible.
The data argument supplies one-dimensional array-like labels. dtype can request the output dtype, name stores an index name, copy controls whether the input is copied, and tupleize_cols controls possible MultiIndex creation.
Follow-up: What happens if dtype is omitted? pandas infers the dtype from the supplied data.
Index objects are immutable and require hashable elements, which makes them suitable for stable axis labels and alignment. They support numeric dtypes broadly, except that numpy.float16 cannot be held.
Follow-up: Can an Index contain mutable Python objects? No; mutable objects are not hashable and therefore cannot be Index elements.
DataFrameSeriesMultiIndexRangeIndexCategoricalIndexIntervalIndexDatetimeIndexTimedeltaIndex