How to replace items that satisfy a condition with another value in numpy array?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

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

0

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.

Relevant Projects

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.