ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.4 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/ |
| Last Crawled | 2026-04-10 09:50:05 (12 days ago) |
| First Indexed | 2021-04-19 23:18:43 (5 years ago) |
| HTTP Status Code | 200 |
| Content | |
| Meta Title | How to Remove Nan Values From a NumPy Array | Delft Stack |
| Meta Description | Learn how to remove nan values from a NumPy array in Python |
| Meta Canonical | null |
| Boilerpipe Text | Remove Nan Values Using
logical_not()
and
isnan()
Methods in NumPy
Remove Nan Values Using the
isfinite()
Method in NumPy
Remove Nan Values Using the
math.isnan
Method
Remove Nan Values Using the
pandas.isnull
Method
This article will discuss some in-built NumPy functions that you can use to delete
nan
values.
Remove Nan Values Using
logical_not()
and
isnan()
Methods in NumPy
logical_not()
is used to apply logical
NOT
to elements of an array.
isnan()
is a boolean function that checks whether an element is
nan
or not.
Using the
isnan()
function, we can create a boolean array that has
False
for all the non
nan
values and
True
for all the
nan
values. Next, using the
logical_not()
function, We can convert
True
to
False
and vice versa.
Lastly, using boolean indexing, We can filter all the non
nan
values from the original NumPy array. All the indexes with
True
as their value will be used to filter the NumPy array.
To learn more about these functions in-depth, refer to their
official documentation
and
here
, respectively.
Refer to the following code snippet for the solution.
import
numpy
as
np
myArray
=
np
.
array([
1
,
2
,
3
, np
.
nan, np
.
nan,
4
,
5
,
6
, np
.
nan,
7
,
8
,
9
, np
.
nan])
output1
=
myArray[np
.
logical_not(np
.
isnan(myArray))]
# Line 1
output2
=
myArray[
~
np
.
isnan(myArray)]
# Line 2
print
(myArray)
print
(output1)
print
(output2)
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
Line 2
is a simplified version of
Line 1
.
Remove Nan Values Using the
isfinite()
Method in NumPy
As the name suggests, the
isfinite()
function is a boolean function that checks whether an element is finite or not. It can also check for finite values in an array and returns a boolean array for the same. The boolean array will store
False
for all the
nan
values and
True
for all the finite values.
We will use this function to retrieve a boolean array for the target array. Using boolean indexing, We will filter all the finite values. Again, as mentioned above, indexes with
True
values will be used to filter the array.
Here’s the example code.
import
numpy
as
np
myArray1
=
np
.
array([
1
,
2
,
3
, np
.
nan, np
.
nan,
4
,
5
,
6
, np
.
nan,
7
,
8
,
9
, np
.
nan])
myArray2
=
np
.
array([np
.
nan, np
.
nan, np
.
nan, np
.
nan, np
.
nan, np
.
nan])
myArray3
=
np
.
array([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
])
output1
=
myArray1[np
.
isfinite(myArray1)]
output2
=
myArray2[np
.
isfinite(myArray2)]
output3
=
myArray3[np
.
isfinite(myArray3)]
print
(myArray1)
print
(myArray2)
print
(myArray3)
print
(output1)
print
(output2)
print
(output3)
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
To learn more about this function, refer to the
official documentation
Remove Nan Values Using the
math.isnan
Method
Apart from these two NumPy solutions, there are two more ways to remove
nan
values. These two ways involve
isnan()
function from
math
library and
isnull
function from
pandas
library. Both these functions check whether an element is
nan
or not and return a boolean result.
Here is the solution using
isnan()
method.
import
numpy
as
np
import
math
myArray1
=
np
.
array([
1
,
2
,
3
, np
.
nan, np
.
nan,
4
,
5
,
6
, np
.
nan,
7
,
8
,
9
, np
.
nan])
myArray2
=
np
.
array([np
.
nan, np
.
nan, np
.
nan, np
.
nan, np
.
nan, np
.
nan])
myArray3
=
np
.
array([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
])
booleanArray1
=
[
not
math
.
isnan(number)
for
number
in
myArray1]
booleanArray2
=
[
not
math
.
isnan(number)
for
number
in
myArray2]
booleanArray3
=
[
not
math
.
isnan(number)
for
number
in
myArray3]
print
(myArray1)
print
(myArray2)
print
(myArray3)
print
(myArray1[booleanArray1])
print
(myArray2[booleanArray2])
print
(myArray3[booleanArray3])
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
Remove Nan Values Using the
pandas.isnull
Method
Below is the solution using the
isnull()
method
from
pandas
.
import
numpy
as
np
import
pandas
as
pd
myArray1
=
np
.
array([
1
,
2
,
3
, np
.
nan, np
.
nan,
4
,
5
,
6
, np
.
nan,
7
,
8
,
9
, np
.
nan])
myArray2
=
np
.
array([np
.
nan, np
.
nan, np
.
nan, np
.
nan, np
.
nan, np
.
nan])
myArray3
=
np
.
array([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
])
booleanArray1
=
[
not
pd
.
isnull(number)
for
number
in
myArray1]
booleanArray2
=
[
not
pd
.
isnull(number)
for
number
in
myArray2]
booleanArray3
=
[
not
pd
.
isnull(number)
for
number
in
myArray3]
print
(myArray1)
print
(myArray2)
print
(myArray3)
print
(myArray1[booleanArray1])
print
(myArray2[booleanArray2])
print
(myArray3[booleanArray3])
print
(myArray1[
~
pd
.
isnull(myArray1)])
# Line 1
print
(myArray2[
~
pd
.
isnull(myArray2)])
# Line 2
print
(myArray3[
~
pd
.
isnull(myArray3)])
# Line 3
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides.
Subscribe |
| Markdown | [](https://www.delftstack.com/)
- [Tutorials](https://www.delftstack.com/tutorial/)
- [HowTos](https://www.delftstack.com/howto/)
- [Reference](https://www.delftstack.com/api/)
- [en](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/)
- [DE](https://www.delftstack.com/de/howto/numpy/numpy-remove-nan-values/)
- [ES](https://www.delftstack.com/es/howto/numpy/numpy-remove-nan-values/)
- [FR](https://www.delftstack.com/fr/howto/numpy/numpy-remove-nan-values/)
- [IT](https://www.delftstack.com/it/howto/numpy/numpy-remove-nan-values/)
- [JA](https://www.delftstack.com/ja/howto/numpy/numpy-remove-nan-values/)
- [KO](https://www.delftstack.com/ko/howto/numpy/numpy-remove-nan-values/)
- [PT](https://www.delftstack.com/pt/howto/numpy/numpy-remove-nan-values/)
- [ZH](https://www.delftstack.com/zh/howto/numpy/numpy-remove-nan-values/)
- [ZH-TW](https://www.delftstack.com/zh-tw/howto/numpy/numpy-remove-nan-values/)
[Tutorial](https://www.delftstack.com/tutorial/)
[Python 3 Basic](https://www.delftstack.com/tutorial/python-3-basic-tutorial/) [Tkinter](https://www.delftstack.com/tutorial/tkinter-tutorial/) [Python Modules](https://www.delftstack.com/tutorial/python-modules-tutorial/) [JavaScript](https://www.delftstack.com/tutorial/javascript/)[Python Numpy](https://www.delftstack.com/tutorial/python-numpy/)[Git](https://www.delftstack.com/tutorial/git/)[Matplotlib](https://www.delftstack.com/tutorial/matplotlib/)[PyQt5](https://www.delftstack.com/tutorial/pyqt5/)[Data Structure](https://www.delftstack.com/tutorial/data-structure/)[Algorithm](https://www.delftstack.com/tutorial/algorithm/)
[HowTo](https://www.delftstack.com/howto/)
[Python Scipy](https://www.delftstack.com/howto/python-scipy/)[Python](https://www.delftstack.com/howto/python/)[Python Tkinter](https://www.delftstack.com/howto/python-tkinter/)[Batch](https://www.delftstack.com/howto/batch/)[PowerShell](https://www.delftstack.com/howto/powershell/)[Python Pandas](https://www.delftstack.com/howto/python-pandas/)[Numpy](https://www.delftstack.com/howto/numpy/)[Python Flask](https://www.delftstack.com/howto/python-flask/)[Django](https://www.delftstack.com/howto/django/)[Matplotlib](https://www.delftstack.com/howto/matplotlib/)[Docker](https://www.delftstack.com/howto/docker/)[Plotly](https://www.delftstack.com/howto/plotly/)[Seaborn](https://www.delftstack.com/howto/seaborn/)[Matlab](https://www.delftstack.com/howto/matlab/)[Linux](https://www.delftstack.com/howto/linux/)[Git](https://www.delftstack.com/howto/git/)[C](https://www.delftstack.com/howto/c/)[Cpp](https://www.delftstack.com/howto/cpp/)[HTML](https://www.delftstack.com/howto/html/)[JavaScript](https://www.delftstack.com/howto/javascript/)[jQuery](https://www.delftstack.com/howto/jquery/)[Python Pygame](https://www.delftstack.com/howto/python-pygame/)[TensorFlow](https://www.delftstack.com/howto/tensorflow/)[TypeScript](https://www.delftstack.com/howto/typescript/)[Angular](https://www.delftstack.com/howto/angular/)[React](https://www.delftstack.com/howto/react/)[CSS](https://www.delftstack.com/howto/css/)[PHP](https://www.delftstack.com/howto/php/)[Java](https://www.delftstack.com/howto/java/)[Go](https://www.delftstack.com/howto/go/)[Kotlin](https://www.delftstack.com/howto/kotlin/)[Node.js](https://www.delftstack.com/howto/node.js/)[Csharp](https://www.delftstack.com/howto/csharp/)[Rust](https://www.delftstack.com/howto/rust/)[Ruby](https://www.delftstack.com/howto/ruby/)[Arduino](https://www.delftstack.com/howto/arduino/)[MySQL](https://www.delftstack.com/howto/mysql/)[MongoDB](https://www.delftstack.com/howto/mongodb/)[Postgres](https://www.delftstack.com/howto/postgres/)[SQLite](https://www.delftstack.com/howto/sqlite/)[R](https://www.delftstack.com/howto/r/)[VBA](https://www.delftstack.com/howto/vba/)[Scala](https://www.delftstack.com/howto/scala/)[Raspberry Pi](https://www.delftstack.com/howto/raspberry-pi/)
[Reference](https://www.delftstack.com/api/)
[Python](https://www.delftstack.com/api/python/)[Python Pandas](https://www.delftstack.com/api/python-pandas/)[Numpy](https://www.delftstack.com/api/numpy/)[Scipy](https://www.delftstack.com/api/scipy/)[JavaScript](https://www.delftstack.com/api/javascript/)
# How to Remove Nan Values From a NumPy Array
[Vaibhav Vaibhav](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#author) Feb 02, 2024 [NumPy](https://www.delftstack.com/tags/numpy/) [NumPy Nan](https://www.delftstack.com/tags/numpy-nan/)
1. [Remove Nan Values Using `logical_not()` and `isnan()` Methods in NumPy](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-logical_not-and-isnan-methods-in-numpy)
2. [Remove Nan Values Using the `isfinite()` Method in NumPy](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-the-isfinite-method-in-numpy)
3. [Remove Nan Values Using the `math.isnan` Method](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-the-mathisnan-method)
4. [Remove Nan Values Using the `pandas.isnull` Method](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-the-pandasisnull-method)

This article will discuss some in-built NumPy functions that you can use to delete `nan` values.
## Remove Nan Values Using `logical_not()` and `isnan()` Methods in NumPy
`logical_not()` is used to apply logical `NOT` to elements of an array. `isnan()` is a boolean function that checks whether an element is `nan` or not.
Using the `isnan()` function, we can create a boolean array that has `False` for all the non `nan` values and `True` for all the `nan` values. Next, using the `logical_not()` function, We can convert `True` to `False` and vice versa.
Lastly, using boolean indexing, We can filter all the non `nan` values from the original NumPy array. All the indexes with `True` as their value will be used to filter the NumPy array.
To learn more about these functions in-depth, refer to their [official documentation](https://numpy.org/doc/stable/reference/generated/numpy.logical_not.html#numpy.logical_not) and [here](https://numpy.org/doc/stable/reference/generated/numpy.isnan.html#numpy.isnan), respectively.
Refer to the following code snippet for the solution.
```
import numpy as np
myArray = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
output1 = myArray[np.logical_not(np.isnan(myArray))] # Line 1
output2 = myArray[~np.isnan(myArray)] # Line 2
print(myArray)
print(output1)
print(output2)
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
```
`Line 2` is a simplified version of `Line 1`.
## Remove Nan Values Using the `isfinite()` Method in NumPy
As the name suggests, the `isfinite()` function is a boolean function that checks whether an element is finite or not. It can also check for finite values in an array and returns a boolean array for the same. The boolean array will store `False` for all the `nan` values and `True` for all the finite values.
We will use this function to retrieve a boolean array for the target array. Using boolean indexing, We will filter all the finite values. Again, as mentioned above, indexes with `True` values will be used to filter the array.
Here’s the example code.
```
import numpy as np
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
output1 = myArray1[np.isfinite(myArray1)]
output2 = myArray2[np.isfinite(myArray2)]
output3 = myArray3[np.isfinite(myArray3)]
print(myArray1)
print(myArray2)
print(myArray3)
print(output1)
print(output2)
print(output3)
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
```
To learn more about this function, refer to the [official documentation](https://numpy.org/doc/stable/reference/generated/numpy.isfinite.html#numpy.isfinite)
## Remove Nan Values Using the `math.isnan` Method
Apart from these two NumPy solutions, there are two more ways to remove `nan` values. These two ways involve `isnan()` function from `math` library and `isnull` function from `pandas` library. Both these functions check whether an element is `nan` or not and return a boolean result.
Here is the solution using `isnan()` method.
```
import numpy as np
import math
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
booleanArray1 = [not math.isnan(number) for number in myArray1]
booleanArray2 = [not math.isnan(number) for number in myArray2]
booleanArray3 = [not math.isnan(number) for number in myArray3]
print(myArray1)
print(myArray2)
print(myArray3)
print(myArray1[booleanArray1])
print(myArray2[booleanArray2])
print(myArray3[booleanArray3])
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
```
## Remove Nan Values Using the `pandas.isnull` Method
Below is the solution using the [`isnull()` method](https://www.delftstack.com/api/python-pandas/pandas-dataframe-dataframe.isnull-and-notnull-function/) from `pandas`.
```
import numpy as np
import pandas as pd
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
booleanArray1 = [not pd.isnull(number) for number in myArray1]
booleanArray2 = [not pd.isnull(number) for number in myArray2]
booleanArray3 = [not pd.isnull(number) for number in myArray3]
print(myArray1)
print(myArray2)
print(myArray3)
print(myArray1[booleanArray1])
print(myArray2[booleanArray2])
print(myArray3[booleanArray3])
print(myArray1[~pd.isnull(myArray1)]) # Line 1
print(myArray2[~pd.isnull(myArray2)]) # Line 2
print(myArray3[~pd.isnull(myArray3)]) # Line 3
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
```
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. [Subscribe](https://www.youtube.com/@delftstack/?sub_confirmation=1)
Author: [**Vaibhav Vaibhav**](https://www.delftstack.com/author/vaibhav/)
[](https://www.delftstack.com/author/vaibhav/) [](https://www.delftstack.com/author/vaibhav/)
Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.
Copyright © 2025. All right reserved
- [About US](https://www.delftstack.com/about-us/)
- [Write For Us](https://www.delftstack.com/write-for-us/)
- [Privacy Policy](https://www.delftstack.com/privacy-policy/)
- [Advertising](https://www.delftstack.com/advertising/)
- [Contact](https://www.delftstack.com/contact/) |
| Readable Markdown | 1. [Remove Nan Values Using `logical_not()` and `isnan()` Methods in NumPy](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-logical_not-and-isnan-methods-in-numpy)
2. [Remove Nan Values Using the `isfinite()` Method in NumPy](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-the-isfinite-method-in-numpy)
3. [Remove Nan Values Using the `math.isnan` Method](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-the-mathisnan-method)
4. [Remove Nan Values Using the `pandas.isnull` Method](https://www.delftstack.com/howto/numpy/numpy-remove-nan-values/#remove-nan-values-using-the-pandasisnull-method)

This article will discuss some in-built NumPy functions that you can use to delete `nan` values.
## Remove Nan Values Using `logical_not()` and `isnan()` Methods in NumPy
`logical_not()` is used to apply logical `NOT` to elements of an array. `isnan()` is a boolean function that checks whether an element is `nan` or not.
Using the `isnan()` function, we can create a boolean array that has `False` for all the non `nan` values and `True` for all the `nan` values. Next, using the `logical_not()` function, We can convert `True` to `False` and vice versa.
Lastly, using boolean indexing, We can filter all the non `nan` values from the original NumPy array. All the indexes with `True` as their value will be used to filter the NumPy array.
To learn more about these functions in-depth, refer to their [official documentation](https://numpy.org/doc/stable/reference/generated/numpy.logical_not.html#numpy.logical_not) and [here](https://numpy.org/doc/stable/reference/generated/numpy.isnan.html#numpy.isnan), respectively.
Refer to the following code snippet for the solution.
```
import numpy as np
myArray = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
output1 = myArray[np.logical_not(np.isnan(myArray))] # Line 1
output2 = myArray[~np.isnan(myArray)] # Line 2
print(myArray)
print(output1)
print(output2)
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
```
`Line 2` is a simplified version of `Line 1`.
## Remove Nan Values Using the `isfinite()` Method in NumPy
As the name suggests, the `isfinite()` function is a boolean function that checks whether an element is finite or not. It can also check for finite values in an array and returns a boolean array for the same. The boolean array will store `False` for all the `nan` values and `True` for all the finite values.
We will use this function to retrieve a boolean array for the target array. Using boolean indexing, We will filter all the finite values. Again, as mentioned above, indexes with `True` values will be used to filter the array.
Here’s the example code.
```
import numpy as np
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
output1 = myArray1[np.isfinite(myArray1)]
output2 = myArray2[np.isfinite(myArray2)]
output3 = myArray3[np.isfinite(myArray3)]
print(myArray1)
print(myArray2)
print(myArray3)
print(output1)
print(output2)
print(output3)
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
```
To learn more about this function, refer to the [official documentation](https://numpy.org/doc/stable/reference/generated/numpy.isfinite.html#numpy.isfinite)
## Remove Nan Values Using the `math.isnan` Method
Apart from these two NumPy solutions, there are two more ways to remove `nan` values. These two ways involve `isnan()` function from `math` library and `isnull` function from `pandas` library. Both these functions check whether an element is `nan` or not and return a boolean result.
Here is the solution using `isnan()` method.
```
import numpy as np
import math
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
booleanArray1 = [not math.isnan(number) for number in myArray1]
booleanArray2 = [not math.isnan(number) for number in myArray2]
booleanArray3 = [not math.isnan(number) for number in myArray3]
print(myArray1)
print(myArray2)
print(myArray3)
print(myArray1[booleanArray1])
print(myArray2[booleanArray2])
print(myArray3[booleanArray3])
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
```
## Remove Nan Values Using the `pandas.isnull` Method
Below is the solution using the [`isnull()` method](https://www.delftstack.com/api/python-pandas/pandas-dataframe-dataframe.isnull-and-notnull-function/) from `pandas`.
```
import numpy as np
import pandas as pd
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
booleanArray1 = [not pd.isnull(number) for number in myArray1]
booleanArray2 = [not pd.isnull(number) for number in myArray2]
booleanArray3 = [not pd.isnull(number) for number in myArray3]
print(myArray1)
print(myArray2)
print(myArray3)
print(myArray1[booleanArray1])
print(myArray2[booleanArray2])
print(myArray3[booleanArray3])
print(myArray1[~pd.isnull(myArray1)]) # Line 1
print(myArray2[~pd.isnull(myArray2)]) # Line 2
print(myArray3[~pd.isnull(myArray3)]) # Line 3
```
Output:
```
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
```
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. [Subscribe](https://www.youtube.com/@delftstack/?sub_confirmation=1) |
| ML Classification | |
| ML Categories | null |
| ML Page Types | null |
| ML Intent Types | null |
| Content Metadata | |
| Language | en |
| Author | null |
| Publish Time | 2021-04-19 00:00:00 (5 years ago) |
| Original Publish Time | 2021-04-19 00:00:00 (5 years ago) |
| Republished | No |
| Word Count (Total) | 1,264 |
| Word Count (Content) | 1,094 |
| Links | |
| External Links | 6 |
| Internal Links | 82 |
| Technical SEO | |
| Meta Nofollow | No |
| Meta Noarchive | No |
| JS Rendered | No |
| Redirect Target | null |
| Performance | |
| Download Time (ms) | 439 |
| TTFB (ms) | 322 |
| Download Size (bytes) | 6,749 |
| Shard | 63 (laksa) |
| Root Hash | 2049407205336873863 |
| Unparsed URL | com,delftstack!www,/howto/numpy/numpy-remove-nan-values/ s443 |