Python Pandas - Return a new Index of the values selected by the indices

To return a new Index of the values selected by the indices, use the index.take() method in Pandas. The take() method allows you to select elements from an Index using their positional indices.

Creating a Pandas Index

First, let's create a Pandas Index with some sample data ?

import pandas as pd

# Creating Pandas index
index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name='Products')

# Display the Pandas index
print("Pandas Index...\n", index)
Pandas Index...
 Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products')

Using take() to Select Values by Index Positions

The take() method accepts a list of integer positions and returns a new Index containing the values at those positions ?

import pandas as pd

# Creating Pandas index
index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name='Products')

# Getting a new index of the values selected by indices
new_index = index.take([1, 2])
print("Selected values at positions 1 and 2...\n", new_index)

# You can also use negative indices
negative_selection = index.take([-1, -2])
print("\nUsing negative indices (-1, -2)...\n", negative_selection)
Selected values at positions 1 and 2...
 Index(['Accessories', 'Decor'], dtype='object', name='Products')

Using negative indices (-1, -2)...
 Index(['Toys', 'Books'], dtype='object', name='Products')

Complete Example

Here's a comprehensive example showing various uses of the take() method ?

import pandas as pd

# Creating Pandas index
index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name='Products')

# Display the original index
print("Original Pandas Index...\n", index)

# Return basic information about the index
print("\nNumber of elements in the index...\n", index.size)
print("\nThe dtype object...\n", index.dtype)

# Select specific positions
print("\nValues at positions [1, 2]...\n", index.take([1, 2]))

# Select multiple non-consecutive positions
print("\nValues at positions [0, 2, 4]...\n", index.take([0, 2, 4]))

# Select using negative indices
print("\nValues at positions [-1, -3]...\n", index.take([-1, -3]))
Original Pandas Index...
 Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products')

Number of elements in the index...
 5

The dtype object...
 object

Values at positions [1, 2]...
 Index(['Accessories', 'Decor'], dtype='object', name='Products')

Values at positions [0, 2, 4]...
 Index(['Electronics', 'Decor', 'Toys'], dtype='object', name='Products')

Values at positions [-1, -3]...
 Index(['Toys', 'Decor'], dtype='object', name='Products')

Key Points

The take() method has several important characteristics:

  • It returns a new Index object with the same dtype and name as the original
  • Accepts both positive and negative integer indices
  • The order of elements in the result matches the order specified in the input list
  • Duplicate indices are allowed and will result in duplicate values

Conclusion

The take() method is an efficient way to select specific elements from a Pandas Index using their positional indices. It preserves the original Index properties while creating a new Index with only the selected values.

Updated on: 2026-03-26T16:06:28+05:30

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements