NumPy Reset Index of an Array in Python [3 Methods]

Do you want to reset the index of an Array in Python? In this Python article, I will explain how NumPy reset index of an array in Python using different methods with some illustrative examples.

To reset the index of a NumPy array in Python after operations like slicing, using np.arange, or reshaping with flatten(), we can create a new array from the modified one. This process effectively reindexes the elements starting from zero. For instance, after slicing an array, reconstruct it with np.array(sliced_array) to reset its indices. Similarly, use np.arange to generate sequential indices for conditionally selected elements, and after flattening an array, recreate it to reset indices.

NumPy Reset Index of an Array in Python

NumPy arrays in Python are n-dimensional array objects, and each element in the array is accessed by its position or ‘index’. The indexing in NumPy is zero-based, meaning that the index of the first element is 0, the second is 1, and so on.

NumPy Array Reset Index Scenarios

There are three different scenarios in NumPy reset index of an array in Python:

  1. Creating a new array
  2. Using np.arange with conditional selection
  3. Reshaping

Let’s see them one by one using some illustrative examples:

1. Index of a NumPy Array by Creating a new array

When we slice a NumPy array in Python, we can create a new array that references a subset of the original array. However, the indices in the sliced array still correspond to their positions in the original array.

In some cases, we might want to NumPy reset index of an array in Python to start from 0.

For instance:

import numpy as np

temps = np.array([68, 70, 72, 74, 76, 78, 80])
weekend_temps = temps[3:]
reset_index_temps = np.array(weekend_temps)
print("Original indices and temperatures:", list(enumerate(temps)))
print("Reset indices and temperatures:", list(enumerate(reset_index_temps)))

Output:

Original indices and temperatures: [(0, 68), (1, 70), (2, 72), (3, 74), (4, 76), (5, 78), (6, 80)]
Reset indices and temperatures: [(0, 74), (1, 76), (2, 78), (3, 80)]

The screenshot below visually represents the output from running the code in PyCharm.

NumPy to reset index of an array in Python

2. NumPy index reset in Python using np.arange with conditional selection

When we apply a condition to a NumPy array and select elements based on this condition, we might end up with non-sequential indices. This can make NumPy reset index of an array in Python easy.

For example:

import numpy as np

populations = np.array([8.4, 3.9, 2.7, 1.6, 0.7])
large_cities = populations > 2
selected_cities = populations[large_cities]
reset_index_cities = np.arange(1, selected_cities.size + 1)
print("Original indices and populations:", list(enumerate(populations)))
print("Reset indices for large cities:", list(enumerate(selected_cities, start=1)))

Output:

Original indices and populations: [(0, 8.4), (1, 3.9), (2, 2.7), (3, 1.6), (4, 0.7)]
Reset indices for large cities: [(1, 8.4), (2, 3.9), (3, 2.7)]

Below is an image displaying the results of the code execution in the PyCharm environment.

numpy reset index in Python

3. NumPy array indexing with reshaping

In operations like concatenation, reshaping, or flattening, we might want the NumPy reset index of an array in Python.

import numpy as np

scores = np.array([[90, 85, 88], [78, 92, 80], [84, 76, 91]])
flattened_scores = scores.flatten()
reset_index_scores = flattened_scores[:]

print("Original 2D scores array with indices:")
for i, row in enumerate(scores):
    for j, score in enumerate(row):
        print(f"Student {i}, Subject {j}: {score}")
print("Flattened array with reset indices:", list(enumerate(reset_index_scores)))

Output:

Original 2D scores array with indices:
Student 0, Subject 0: 90
Student 0, Subject 1: 85
Student 0, Subject 2: 88
Student 1, Subject 0: 78
Student 1, Subject 1: 92
Student 1, Subject 2: 80
Student 2, Subject 0: 84
Student 2, Subject 1: 76
Student 2, Subject 2: 91
Flattened array with reset indices: [(0, 90), (1, 85), (2, 88), (3, 78), (4, 92), (5, 80), (6, 84), (7, 76), (8, 91)]

The screenshot below showcases the output generated by running the code within the PyCharm editor.

numpy array index reset in Python

Conclusion

Here, this article explains how NumPy reset index of an array in Python with three different scenarios using slicing and creating a new array, using np.arange with conditional selection, or reshaping it using the flatten() function.

The choice of the scenario will depend on the problem we are facing,

You may also like to read: