How to drop all missing values from a numpy array?

This recipe helps you drop all missing values from a numpy array

Recipe Objective

How to drop all missing values from a numpy array?

Droping the missing values or nan values can be done by using the function "numpy.isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy.logical_not()" where the boolean values will be reversed. At last we want the elements which are having non-nan values which can be further filtered out and store it into another array.

Step 1 - Import library

import numpy as np

Step 2 - Take Sample data

Sample_data = np.array([1,2,7,8,np.nan,9,5,np.nan,1,0]) print("This is Sample data with nan values in it:", Sample_data)

This is Sample data with nan values in it: [ 1.  2.  7.  8. nan  9.  5. nan  1.  0.]

Step 3 - Remove Nan values

remove_nan = Sample_data[np.logical_not(np.isnan(Sample_data))]

Step 4 - Print Results

print("This is the original data with nan values:", Sample_data, "\n") print("This is the data without nan values:", remove_nan)

This is the original data with nan values: [ 1.  2.  7.  8. nan  9.  5. nan  1.  0.] 

This is the data without nan values: [1. 2. 7. 8. 9. 5. 1. 0.]

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Build a Langchain Streamlit Chatbot for EDA using LLMs
In this LLM project, you will build a Streamlit Chatbot integrated with Langchain technology for natural language interactions with a SQL database, facilitating real-time visualization and insightful insights, streamlining data exploration and analysis.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

OSZAR »