How to replace items that satisfy a condition with another value in numpy array?

This recipe helps you replace items that satisfy a condition with another value in numpy array

Recipe Objective

How to replace items that satisfy a condition with another value in numpy array? This is possible in the Numpy by using the "where" condition.

Step 1 - Import library

import numpy as np

Step 2 - Take Sample array

Sample_array = np.array([10,20,30,40,45,50,55,60,65,70,75,80]) print("This is the Sample array:", Sample_array)
This is the Sample array: [10 20 30 40 45 50 55 60 65 70 75 80]

Step 3 - Print the Results

Result = np.where(Sample_array > 45, 0, Sample_array) print("This is Resulted array where we have applied condition:", Result)
This is Resulted array where we have applied condition: [10 20 30 40 45  0  0  0  0  0  0  0]

Here we have applied a condition where if the number is greater than 45 then it will be 0.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.