Python - Return an array representing the data in the Pandas Index

To return an array representing the data in the Pandas Index, use the index.values property. This converts the Index object to a NumPy array containing the underlying data.

Basic Usage

First, let's create a simple Index and extract its values ?

import pandas as pd

# Creating the index
index = pd.Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'])

# Display the index
print("Pandas Index:")
print(index)

# Return an array representing the data in the Index
print("\nArray:")
print(index.values)
print("Type:", type(index.values))
Pandas Index:
Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object')

Array:
['Car' 'Bike' 'Truck' 'Ship' 'Airplane']
Type: <class 'numpy.ndarray'>

Different Index Types

The values property works with various Index types ?

import pandas as pd
import numpy as np

# Numeric Index
numeric_index = pd.Index([10, 20, 30, 40, 50])
print("Numeric Index values:")
print(numeric_index.values)

# DateTime Index
date_index = pd.date_range('2024-01-01', periods=3)
print("\nDateTime Index values:")
print(date_index.values)

# MultiIndex
multi_index = pd.MultiIndex.from_tuples([('A', 1), ('B', 2), ('C', 3)])
print("\nMultiIndex values:")
print(multi_index.values)
Numeric Index values:
[10 20 30 40 50]

DateTime Index values:
['2024-01-01T00:00:00.000000000' '2024-01-02T00:00:00.000000000'
 '2024-01-03T00:00:00.000000000']

MultiIndex values:
[('A', 1) ('B', 2) ('C', 3)]

Using with DataFrame

Extract array values from DataFrame row and column indices ?

import pandas as pd

# Create DataFrame with custom indices
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}, index=['row1', 'row2', 'row3'])

print("DataFrame:")
print(df)

print("\nRow index values:")
print(df.index.values)

print("\nColumn index values:")
print(df.columns.values)
DataFrame:
      A  B
row1  1  4
row2  2  5
row3  3  6

Row index values:
['row1' 'row2' 'row3']

Column index values:
['A' 'B']

Key Points

  • index.values returns a NumPy array, not a Pandas Index
  • The array preserves the original data types
  • Works with all Index types: RangeIndex, DatetimeIndex, MultiIndex, etc.
  • Useful for operations that require NumPy arrays instead of Pandas objects

Conclusion

The index.values property provides a simple way to convert Pandas Index objects to NumPy arrays. This is particularly useful when you need to perform NumPy operations or interface with libraries that expect array inputs.

Updated on: 2026-03-26T16:12:31+05:30

507 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements