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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

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.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

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.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.