Pandas Index Object

Pandas' Index object is a base class used to identify axis labels, providing rich functionality to represent and manage data indexes.

Here are some key features and uses of the Index object:

  • Unique identification:IndexThe object provides unique identifiers for data, which is crucial for data selection and manipulation.
  • Label-based: Unlike position-based indexing (such as indexing in Python lists),IndexIt allows label-based indexing, making data manipulation more intuitive and flexible.
  • Data type support:IndexIt can hold multiple types of data, including integers, floats, strings, date-time, etc.

Pandas provides several different Index types for different scenarios:

  • RangeIndex: A memory-efficient integer value index object, similar to Python'srangeobject.
  • Index: The most basic Index type, which can contain any type of data.
  • MultiIndex: A multi-level index that allows you to have multiple index levels, similar to multiple columns in a DataFrame.
  • DatetimeIndex: An index optimized for date-time data, providing date-time related functionality.
  • PeriodIndex: A period-based index, such as year, quarter, etc.
  • TimedeltaIndex: An index based on time delta (Δt).

Index Constructor

Class/Method Description
pd.Index(data, dtype, name) Create an Index object, supporting custom data, data types, and name.

Index Properties

Attribute Description
Index.values Return the data portion of the Index (numpy array).
Index.dtype Return the data type of the Index.
Index.name Return or set the name of the Index.
Index.shape Return the shape of the Index (as a tuple).
Index.size Return the number of elements in the Index.
Index.nlevels Return the number of levels in the Index (for MultiIndex).
Index.is_unique Check whether the values in the Index are unique.
Index.is_monotonic Check whether the Index is monotonically increasing.
Index.is_monotonic_decreasing Check whether the Index is monotonically decreasing.
Index.has_duplicates Check whether the Index has duplicate values.
Index.empty Check whether the Index is empty.

Index Methods

Data Operations

Method Description
Index.append(other) Append another Index to the current Index.
Index.drop(labels) Delete the specified label.
Index.insert(loc, item) Insert an element at the specified position.
Index.unique() Return the unique values in the Index.
Index.drop_duplicates() Remove duplicate values.
Index.sort_values() Sort by value.
Index.sort_values(ascending=False) Sort by value in descending order.
Index.tolist() Convert the Index to a list.
Index.to_numpy() Convert the Index to a numpy array.
Index.to_frame() Convert the Index to a DataFrame.
Index.astype(dtype) Convert the Index to the specified data type.
Index.map(func) Apply a function to each element in the Index.
Index.where(cond, other) Replace values based on a condition.
Index.mask(cond, other) Replace values based on a condition (as opposed towhere).

Indexing Operations

Method Description
Index.get_loc(key) Return the position of the specified label.
Index.get_indexer(target) Return the position of the target Index in the current Index.
Index.slice_locs(start, end) Return the slice position for the specified range.
Index.intersection(other) Return the intersection of two Indexes.
Index.union(other) Return the union of two Indexes.
Index.difference(other) Return the difference of two Indexes.
Index.symmetric_difference(other) Return the symmetric difference of two Indexes.
Index.isin(values) Check whether the values in the Index are in the specified list.
Index.reindex(target) Reindex according to the target Index.
Index.reindex_like(other) Reindex according to another Index.

Statistical Computation

Method Description
Index.min() Return the minimum value in the Index.
Index.max() Return the maximum value in the Index.
Index.argmin() Return the index position of the minimum value.
Index.argmax() Return the index position of the maximum value.
Index.value_counts() Return the frequency of each value in the Index.

MultiIndex Methods

Method Description
pd.MultiIndex.from_arrays() Create a MultiIndex from an array.
pd.MultiIndex.from_tuples() Create a MultiIndex from tuples.
pd.MultiIndex.from_product() Create a MultiIndex from the Cartesian product.
MultiIndex.levels Return the levels of the MultiIndex.
MultiIndex.codes Return the codes of the MultiIndex.
MultiIndex.swaplevel(i, j) Swap the positions of two levels.
MultiIndex.droplevel(level) Remove the specified level.
MultiIndex.set_levels(levels) Set the levels of the MultiIndex.
MultiIndex.set_codes(codes) Set the codes of the MultiIndex.

Example

Example

import pandas as pd

# Create Index
idx = pd.Index([1, 2, 3], name='MyIndex')

# View properties
print(idx.values)  # Output data portion
print(idx.name)    # Output name

# Data operations
idx_new = idx.append(pd.Index([4, 5]))
print(idx_new)  # Output the appended Index

# Indexing operations
print(idx.get_loc(2))  # Output the position of label 2

# MultiIndex operations
arrays = [[1, 1, 2, 2], ['A', 'B', 'A', 'B']]
multi_idx = pd.MultiIndex.from_arrays(arrays, names=('Num', 'Letter'))
print(multi_idx)

For more detailed information, please refer toPandas official documentation。

Other extensions