NumPy Reset Index

Recently, I was working on a data analysis project where I needed to reset the index of a NumPy array after filtering out some values. The issue is, there’s no direct “reset_index()” method in NumPy like we have in pandas. So we need a workaround.

In this article, I’ll cover several simple ways you can use to reset or reindex NumPy arrays in Python (including basic indexing techniques and more advanced approaches).

So let’s get in!

NumPy Reset Index

Now, I will explain to you the methods to reset the index in Python NumPy.

Read np.genfromtxt() Function in Python

Method 1 – Use Simple Slicing

NumPy arrays are indexed differently from pandas DataFrames. When you filter a Python NumPy array, it creates a view or copy with only the selected elements, but doesn’t maintain any concept of the original indices.

Let’s see this with an example:

import numpy as np

# Create a sample array
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

# Filter values greater than 50
filtered_array = data[data > 50]
print("Filtered array:", filtered_array)

Output:

Filtered array: [60 70 80 90]

You can see the output in the screenshot below.

numpy reset index

The filtered array now contains only the values greater than 50, but there’s no built-in way to know these came from indices 5, 6, 7, and 8 of the original array. If you need to maintain this relationship, here’s how you can do it:

import numpy as np

# Create a sample array
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

# Get both indices and values where condition is met
indices = np.where(data > 50)[0]
values = data[indices]

print("Original indices:", indices)
print("Values:", values)

# If you need a "reset" index (0, 1, 2, 3...)
new_indices = np.arange(len(values))
print("New indices:", new_indices)

Output:

Original indices: [5 6 7 8]
Values: [60 70 80 90]
New indices: [0 1 2 3]

This approach gives you both the original indices and the values, along with a new sequential index starting from 0.

Check out np.savetxt() Function in Python

Method 2 – Use numpy.nonzero()

Another approach is to use np.nonzero() which returns the indices of non-zero elements in the array in Python. We can combine this with a boolean mask to get indices where our condition is true:

import numpy as np

# Create a sample array
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

# Get indices of elements that satisfy the condition
condition = data > 50
indices = np.nonzero(condition)[0]
values = data[indices]

print("Original indices:", indices)
print("Values:", values)

# Reset indices to start from 0
result = np.column_stack((np.arange(len(indices)), values))
print("Array with reset indices:\n", result)

Output:

Original indices: [5 6 7 8]
Values: [60 70 80 90]
Array with reset indices:
 [[ 0 60]
 [ 1 70]
 [ 2 80]
 [ 3 90]]

You can see the output in the screenshot below.

import numpy as np

This gives us a 2D array where the first column contains the new indices (0, 1, 2, 3) and the second column contains our values.

Read NumPy Array to List in Python

Method 3 – Create a Structured Array

If you want something more similar to pandas’ behavior, you can create a structured array that keeps track of both indices and values in Python:

import numpy as np

# Create a sample array
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

# Filter values
mask = data > 50
filtered_values = data[mask]

# Create a structured array with original index and value
original_indices = np.where(mask)[0]
structured_array = np.zeros(len(filtered_values), dtype=[('original_index', int), ('value', int)])
structured_array['original_index'] = original_indices
structured_array['value'] = filtered_values

print("Structured array with original indices:")
print(structured_array)

# "Reset" the index
reset_structured_array = np.zeros(len(filtered_values), dtype=[('new_index', int), ('original_index', int), ('value', int)])
reset_structured_array['new_index'] = np.arange(len(filtered_values))
reset_structured_array['original_index'] = original_indices
reset_structured_array['value'] = filtered_values

print("\nStructured array with reset indices:")
print(reset_structured_array)

Output:

Structured array with original indices:
[(5, 60) (6, 70) (7, 80) (8, 90)]

Structured array with reset indices:
[(0, 5, 60) (1, 6, 70) (2, 7, 80) (3, 8, 90)]

You can see the output in the screenshot below.

index of numpy

This method gives you a structured array that maintains both the original indices and the new reset indices, along with the values.

Check out NumPy Reverse Array in Python

Method 4 – Use a Custom Function

For convenience, we can create a custom function to handle the index resetting:

import numpy as np

def reset_index(arr, condition=None):
    """
    Reset index of a NumPy array, optionally filtering by a condition.
    Returns a structured array with new_index, original_index, and value.
    """
    if condition is not None:
        original_indices = np.where(condition)[0]
        values = arr[condition]
    else:
        original_indices = np.arange(len(arr))
        values = arr

    result = np.zeros(len(values), dtype=[('new_index', int), ('original_index', int), ('value', int)])
    result['new_index'] = np.arange(len(values))
    result['original_index'] = original_indices
    result['value'] = values

    return result

# Usage example
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])
result = reset_index(data, data > 50)
print(result)

Output:

[(0, 5, 60) (1, 6, 70) (2, 7, 80) (3, 8, 90)]

This function gives you a clean way to reset the index of any NumPy array while preserving the original indices if needed.

Read NumPy Array to a String in Python

Method 5 – Use Pandas to Reset Index

If you’re working in a data analysis context where you’re likely already using pandas, you can leverage pandas’ reset_index method:

import numpy as np
import pandas as pd

# Create a sample array
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

# Convert to pandas Series, filter, and reset index
series = pd.Series(data)
filtered_series = series[series > 50].reset_index()
filtered_series.columns = ['original_index', 'value']

# Add a new reset index
filtered_series['new_index'] = np.arange(len(filtered_series))

print(filtered_series)

# Convert back to NumPy if needed
result_array = filtered_series.to_numpy()
print("\nAs NumPy array:")
print(result_array)

Output:

   original_index  value  new_index
0               5     60          0
1               6     70          1
2               7     80          2
3               8     90          3

As NumPy array:
[[5 60  0]
 [6 70  1]
 [7 80  2]
 [8 90  3]]

While this approach involves converting between NumPy and pandas, it can be very convenient if you already use pandas in your workflow.

I hope you found this article helpful. The key takeaway is that NumPy doesn’t have a direct “reset_index” function like pandas, but there are several ways to achieve the same result depending on your needs. Whether you want to create a new sequential index, keep track of the original indices, or create a more structured representation, the above methods should cover most use cases.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.