# Remove null values from a numpy array in Python

In this tutorial, you will learn **how to remove null values from a numpy array in Python**.

**Numpy Package:**

We use the Numerical Python(numpy) module for computation and processing arrays. Numpy array is a multidimensional object which consists of rows and columns used for computation. Now let’s have a glance at how to remove null values from a numpy array in python.

**Example :
**

#create arrays using numpy import numpy as np arr=np.array([[1,2,3],[4,5,6]]) # creating an array print(arr)

Output:

[[1,2,3] [4,5,6]]

So, in the above example as numpy is a package it is to import in the script. Hence we create an array using np.array(). As a result, elements are stored as an array in the 2-dimensional format in **arr.** In the final step of our program, we print the array **arr**. As we created a 2-dimensional array the output of **arr **is in the 2-dimensional format.

In the above program, we came to know how to create a numpy array. Now in the future program let’s have a glance over creating an array with null values and how to remove null values from that numpy array :

## How to remove null values from a numpy array in Python

import numpy as em arr=em.array([1,2,3,4,em.nan,5,6,em.nan]) #creating array print(arr) arr=arr[em.logical_not(em.isnan(arr))] #removing null values print(arr)

Output:

[ 1. 2. 3. 4. nan 5. 6. nan] [1. 2. 3. 4. 5. 6.]

**Explanation:**

From the above code initially, we import the numpy package to the script.

- In the first step, we create an array using em.array(), now we print the unmodified array which contains null values.
- In the second step, we remove the null values where em.nan are the null values in the numpy array from the array. Inside the function of em.isnan return a logical array
**T****rue**when arr is not a number. So we use logical not to get only numbers into that array. - So, as the last step in our, script we print the modified array which doesn’t contain the null values.

In this way, we can remove the null values from the numpy array. For further reading on numpy that is performed on arrays–>

Thanks for reading.

## Leave a Reply