When working with NumPy arrays, I often need to replace specific values based on their indices. Whether I’m cleaning data for machine learning models or updating values in a financial analysis, knowing how to replace array elements efficiently is important.
In this article, I’ll show you several easy methods to replace values in NumPy arrays by index. After years of working with Python data analysis, I’ve found these techniques to be the most practical and efficient.
Let’s dive in and explore how to master this essential NumPy operation.
Methods to Replace Values in NumPy Array by Index in Python
Now I will explain some methods to replace values in a NumPy array by index in Python.
Read np.diff() Function in Python
Method 1 – Simple Indexing for Single Value Replacement
The most basic way to replace a value in a NumPy array is by using simple indexing in Python. This works similarly to how you’d replace values in a regular Python list.
Here’s how to replace a single value:
import numpy as np
# Create a sample array
sales_data = np.array([1200, 1500, 900, 1800, 2000])
# Replace the value at index 2 (third element) with 1000
sales_data[2] = 1000
print(sales_data)Output:
[1200 1500 1000 1800 2000]I executed the above example code and added the screenshot below.

In this example, I’ve replaced the third element (index 2) of our sales data array with the value 1000.
This method is perfect when you need to update a single value at a known position.
Check out NumPy Filter 2D Array by Condition in Python
Method 2 – Using Slicing to Replace Multiple Values
When you need to replace multiple consecutive values, slicing is the way to go. It’s efficient and maintains the readability of your code.
import numpy as np
# Create a sample array of monthly website visitors (in thousands)
visitors = np.array([45, 48, 52, 49, 53, 57, 60, 58, 56, 59, 62, 65])
# Replace values for summer months (indices 5, 6, 7) with higher numbers
visitors[5:8] = [70, 75, 72]
print(visitors)Output:
[45 48 52 49 53 70 75 72 56 59 62 65]I executed the above example code and added the screenshot below.

Here, I’ve replaced the visitor numbers for the summer months with higher values using array slicing. This approach is much cleaner than updating each value individually.
Read Use np.argsort in Descending Order in Python
Method 3 – Boolean Indexing for Conditional Replacement
One of NumPy’s most useful features is boolean indexing, which allows you to replace values based on conditions. This is incredibly useful for data cleaning and transformation.
import numpy as np
# Create a sample array of temperature readings (in Fahrenheit)
temperatures = np.array([72, 65, 83, 90, 92, 89, 78, 61, 69, 74])
# Replace all temperatures above 85°F with 85 (perhaps adjusting outliers)
temperatures[temperatures > 85] = 85
print(temperatures)Output:
[72 65 83 85 85 85 78 61 69 74]I executed the above example code and added the screenshot below.

In this example, I’m capping all temperature readings above 85°F to exactly 85°F. This technique is commonly used to handle outliers in data analysis.
Check out Copy Elements from One List to Another in Python
Method 4 – Use np.where() for Conditional Replacement
Python np.where() function provides another elegant way to perform conditional replacements. It works like a vectorized if-else statement.
import numpy as np
# Create a sample array of product ratings (1-5 scale)
ratings = np.array([4.2, 3.8, 2.5, 4.9, 3.1, 1.7, 4.8, 3.2, 2.9, 4.5])
# Replace low ratings (<3.0) with 3.0 (perhaps for a minimum display rating)
ratings = np.where(ratings < 3.0, 3.0, ratings)
print(ratings)
# Output: [4.2 3.8 3. 4.9 3.1 3. 4.8 3.2 3. 4.5]Here, I’m setting a minimum display rating of 3.0 by replacing any value below that threshold. The np.where() function evaluates the condition for each element and returns either the replacement value or the original value.
Read np.count() function in Python
Advanced Example: Replace Multiple Values with Different Replacements
Sometimes, you need to replace different values with different replacements. Here’s how to do that using NumPy’s advanced indexing:
import numpy as np
# Sample array of house prices (in thousands of dollars)
house_prices = np.array([350, 420, 285, 510, 370, 425, 390, 480, 330, 560])
# Indices of houses we want to update
indices = [1, 4, 7]
# New prices for these houses
new_prices = [450, 400, 500]
# Replace the values
house_prices[indices] = new_prices
print(house_prices)
# Output: [350 450 285 510 400 425 390 500 330 560]In this example, I’m updating the prices of specific houses in our dataset by providing the indices and their corresponding new values.
Check out Convert the DataFrame to a NumPy Array Without Index in Python
Practical Application: Clean Stock Price Data
Let’s look at a real-world example of cleaning stock price data:
import numpy as np
# Sample array of daily closing prices for a stock
stock_prices = np.array([142.5, 143.2, 0, 145.1, 144.8, -999, 146.2, 147.5, 0, 148.3])
# Replace missing values (0) with the previous day's price
for i in range(len(stock_prices)):
if stock_prices[i] == 0 and i > 0:
stock_prices[i] = stock_prices[i-1]
# Replace error values (-999) with the average of surrounding days
error_indices = np.where(stock_prices == -999)[0]
for idx in error_indices:
if 0 < idx < len(stock_prices) - 1:
stock_prices[idx] = (stock_prices[idx-1] + stock_prices[idx+1]) / 2
print(stock_prices)
# Output should show the cleaned data with no zeros or -999 valuesIn this practical example, I’m cleaning stock price data by:
- Replacing missing values (0) with the previous day’s price
- Replacing error values (-999) with the average of the surrounding days
These are common data cleaning operations in financial analysis.
I hope these methods help you efficiently replace values in NumPy arrays. Whether you’re working with simple or complex datasets, NumPy provides useful tools to manipulate array values in a clean, efficient manner.
If you have specific replacement needs for your data analysis or processing tasks, these techniques can be combined and customized to fit your requirements.
Other Python articles you may also like:
- np.unit8 in Python
- NumPy Divide Array by Scalar in Python
- Copy a NumPy Array to the Clipboard through Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.