How to replace values in NumPy array by index in Python [4 Ways]

Do you want to replace values in NumPy array? In this NumPy Python tutorial, I will explain how to replace values in NumPy array by index in Python using different methods with examples.

To replace values in a NumPy array by index in Python, use simple indexing for single values (e.g., array[0] = new_value), slicing for multiple values (array[start:end] = new_values_array), boolean indexing for condition-based replacement (array[array > threshold] = new_value), and fancy indexing to change specific positions (array[[index1, index2]] = [new_value1, new_value2]).

Replace values in NumPy array by index in Python

To replace values in NumPy array by index in Python is a fundamental operation in data manipulation and analysis. Here, are the four different functions and methods available in NumPy:

  1. Simple Value Replacement
  2. Replacing Multiple Values
  3. Boolean Indexing
  4. Fancy Indexing

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

1. NumPy replace value in Python

To replace a value in NumPy array by index in Python, assign a new value to the desired index. For instance:

import numpy as np

temperatures = np.array([58, 66, 52, 69, 77])
temperatures[0] = 59
print("Updated Temperatures:", temperatures)

Output:

Updated Temperatures: [59 66 52 69 77]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

replace values in numpy array by index in Python

2. Replace values in NumPy array by replacing multiple values

To replace multiple values, we can use slicing in Python. Slices include the start index and exclude the end index. For instance:

import numpy as np

sunny_days = np.array([152, 259, 245, 233, 294])
sunny_days[1:3] = [260, 250]
print("Updated Sunny Days:", sunny_days)

Output:

Updated Sunny Days: [152 260 250 233 294]

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

numpy replace in python

3. Replace value in NumPy array by boolean indexing

With boolean indexing, we can replace values in NumPy array by index in Python that meet a certain condition. For example:

import numpy as np

sales = np.array([45, 55, 40, 50, 60])
sales[sales < 50] = -1
print("Updated Sales Data:", sales)

Output:

Updated Sales Data: [-1 55 -1 50 60]

After executing the code in Pycharm, one can see the output in the below screenshot.

numpy array replace in Python

4. Replace values in matrix Python with fancy indexing

Fancy indexing involves passing an array of indices to access multiple elements to replace values in NumPy array by index in Python. For example:

import numpy as np

populations = np.array([120, 85, 95, 110, 100])
populations[[0, 4]] = populations[[4, 0]]
print("Updated Populations:", populations)

Output:

Updated Populations: [100  85  95 110 120]

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

numpy replace values in Python

NumPy replace 0 with 1 in a Python array

Let’s see a situation where we have to replace 0 in the array by 1 through Python:

import numpy as np

species_presence = np.array([1, 0, 1, 0, 0])
species_presence[species_presence == 0] = 1
print("Updated Species Presence:", species_presence)

Output:

Updated Species Presence: [1 1 1 1 1]

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

np replace 0 with 1 in Python

Conclusion

This article explains how to replace values in NumPy array by index in Python using four different ways such as simple indexing, multiple values at a time, boolean indexing, and fancy indexing with illustrative examples. I have also explained how in NumPy array, replace 0 with 1.

You may also like to read some articles: