âšī¸ 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://note.nkmk.me/en/python-numpy-nan-remove/ |
| Last Crawled | 2026-03-28 08:07:35 (10 days ago) |
| First Indexed | 2021-06-04 23:12:48 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | NumPy: Remove NaN (np.nan) from an array | note.nkmk.me |
| Meta Description | In NumPy, to remove rows or columns containing NaN (np.nan) from an array (ndarray), use np.isnan() to identify NaN and methods like any() or all() to extract rows or columns that do not contain NaN. ... |
| Meta Canonical | null |
| Boilerpipe Text | In NumPy, to remove rows or columns containing
NaN
(
np.nan
) from an array (
ndarray
), use
np.isnan()
to identify
NaN
and methods like
any()
or
all()
to extract rows or columns that do not contain
NaN
.
Additionally, you can remove all
NaN
values from an array, but this will flatten the array.
Contents
Remove all
NaN
from an array
Remove rows containing
NaN
Remove columns containing
NaN
For basics on handling
NaN
in Python, refer to the following article.
What is nan in Python (float('nan'), math.nan, np.nan)
For replacing
NaN
with other values instead of removing them, refer to the following article.
NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan()
The NumPy version used in this article is as follows. Note that functionality may vary between versions. For example, consider reading the following CSV file, which contains missing data, using
np.genfromtxt()
.
sample_nan.csv
NumPy: Read and write CSV files (np.loadtxt, np.genfromtxt, np.savetxt)
import
numpy
as
np
print
(
np
.
__version__
)
# 1.26.1
a
=
np
.
genfromtxt
(
'data/src/sample_nan.csv'
,
delimiter
=
','
)
print
(
a
)
# [[11. 12. nan 14.]
# [21. nan nan 24.]
# [31. 32. 33. 34.]]
Remove all
NaN
from an array
You can use
np.isnan()
to check if values in an
ndarray
are
NaN
.
numpy.isnan â NumPy v1.26 Manual
a
=
np
.
genfromtxt
(
'data/src/sample_nan.csv'
,
delimiter
=
','
)
print
(
a
)
# [[11. 12. nan 14.]
# [21. nan nan 24.]
# [31. 32. 33. 34.]]
print
(
np
.
isnan
(
a
))
# [[False False True False]
# [False True True False]
# [False False False False]]
Applying the negation operator (
~
) to this resulting
ndarray
turns
NaN
to
False
, which can be used as a mask to remove
NaN
(extract non-NaN values). Since the number of remaining elements changes, the resulting
ndarray
does not retain the same shape as the original
ndarray
, but instead becomes flattened (converted to one-dimensional).
print
(
~
np
.
isnan
(
a
))
# [[ True True False True]
# [ True False False True]
# [ True True True True]]
print
(
a
[
~
np
.
isnan
(
a
)])
# [11. 12. 14. 21. 24. 31. 32. 33. 34.]
Remove rows containing
NaN
To remove rows containing
NaN
, call the
any()
method on the
ndarray
generated by
np.isnan()
. The
any()
method returns
True
if there is at least one
True
in the
ndarray
.
numpy.ndarray.any â NumPy v1.26 Manual
By setting
axis=1
in
any()
, it checks whether there is at least one
True
in each row, indicating the presence of
NaN
.
NumPy: Meaning of the axis parameter (0, 1, -1)
a
=
np
.
genfromtxt
(
'data/src/sample_nan.csv'
,
delimiter
=
','
)
print
(
a
)
# [[11. 12. nan 14.]
# [21. nan nan 24.]
# [31. 32. 33. 34.]]
print
(
np
.
isnan
(
a
))
# [[False False True False]
# [False True True False]
# [False False False False]]
print
(
np
.
isnan
(
a
)
.
any
(
axis
=
1
))
# [ True True False]
Using the negation operator (
~
) to swap
True
and
False
, rows without any
NaN
become
True
.
print
(
~
np
.
isnan
(
a
)
.
any
(
axis
=
1
))
# [False False True]
By applying this
ndarray
to the rows (the first dimension) of the original
ndarray
, you can remove rows with
NaN
(extract rows without
NaN
).
NumPy: Get and set values in an array using various indexing
print
(
a
[
~
np
.
isnan
(
a
)
.
any
(
axis
=
1
),
:])
# [[31. 32. 33. 34.]]
You can omit the column specification (
:
) as shown below.
print
(
a
[
~
np
.
isnan
(
a
)
.
any
(
axis
=
1
)])
# [[31. 32. 33. 34.]]
To remove only rows where all elements are
NaN
, use
all()
instead of
any()
.
numpy.ndarray.all â NumPy v1.26 Manual
Setting
axis=1
checks if all elements in each row are
True
. Here,
np.nan
is assigned to elements for explanation.
a
[
1
,
0
]
=
np
.
nan
a
[
1
,
3
]
=
np
.
nan
print
(
a
)
# [[11. 12. nan 14.]
# [nan nan nan nan]
# [31. 32. 33. 34.]]
print
(
np
.
isnan
(
a
)
.
all
(
axis
=
1
))
# [False True False]
print
(
~
np
.
isnan
(
a
)
.
all
(
axis
=
1
))
# [ True False True]
print
(
a
[
~
np
.
isnan
(
a
)
.
all
(
axis
=
1
)])
# [[11. 12. nan 14.]
# [31. 32. 33. 34.]]
Remove columns containing
NaN
The process to remove columns containing
NaN
is similar to that used for rows.
Using
any()
with
axis=0
checks if there is at least one
True
in each column, indicating the presence of
NaN
. Apply the negation operator (
~
) to convert columns without any
NaN
to
True
.
a
=
np
.
genfromtxt
(
'data/src/sample_nan.csv'
,
delimiter
=
','
)
print
(
a
)
# [[11. 12. nan 14.]
# [21. nan nan 24.]
# [31. 32. 33. 34.]]
print
(
np
.
isnan
(
a
))
# [[False False True False]
# [False True True False]
# [False False False False]]
print
(
np
.
isnan
(
a
)
.
any
(
axis
=
0
))
# [False True True False]
print
(
~
np
.
isnan
(
a
)
.
any
(
axis
=
0
))
# [ True False False True]
By applying this
ndarray
to the columns (the second dimension) of the original
ndarray
, you can remove columns with
NaN
(extract columns without
NaN
).
print
(
a
[:,
~
np
.
isnan
(
a
)
.
any
(
axis
=
0
)])
# [[11. 14.]
# [21. 24.]
# [31. 34.]]
To remove only columns where all elements are
NaN
, use
all()
instead of
any()
.
a
[
2
,
2
]
=
np
.
nan
print
(
a
)
# [[11. 12. nan 14.]
# [21. nan nan 24.]
# [31. 32. nan 34.]]
print
(
np
.
isnan
(
a
)
.
all
(
axis
=
0
))
# [False False True False]
print
(
~
np
.
isnan
(
a
)
.
all
(
axis
=
0
))
# [ True True False True]
print
(
a
[:,
~
np
.
isnan
(
a
)
.
all
(
axis
=
0
)])
# [[11. 12. 14.]
# [21. nan 24.]
# [31. 32. 34.]] |
| Markdown | [note.nkmk.me](https://note.nkmk.me/en/)
1. [Home](https://note.nkmk.me/en/)
2. [Python](https://note.nkmk.me/en/python/)
3. [NumPy](https://note.nkmk.me/en/numpy/)
# NumPy: Remove NaN (np.nan) from an array
Modified:
2024-01-23
\| Tags: [Python](https://note.nkmk.me/en/python/), [NumPy](https://note.nkmk.me/en/numpy/)
In NumPy, to remove rows or columns containing `NaN` (`np.nan`) from an array (`ndarray`), use `np.isnan()` to identify `NaN` and methods like `any()` or `all()` to extract rows or columns that do not contain `NaN`.
Additionally, you can remove all `NaN` values from an array, but this will flatten the array.
Contents
- [Remove all `NaN` from an array](https://note.nkmk.me/en/python-numpy-nan-remove/#remove-all-nan-from-an-array)
- [Remove rows containing `NaN`](https://note.nkmk.me/en/python-numpy-nan-remove/#remove-rows-containing-nan)
- [Remove columns containing `NaN`](https://note.nkmk.me/en/python-numpy-nan-remove/#remove-columns-containing-nan)
For basics on handling `NaN` in Python, refer to the following article.
- [What is nan in Python (float('nan'), math.nan, np.nan)](https://note.nkmk.me/en/python-nan-usage/)
For replacing `NaN` with other values instead of removing them, refer to the following article.
- [NumPy: Replace NaN (np.nan) using np.nan\_to\_num() and np.isnan()](https://note.nkmk.me/en/python-numpy-nan-replace/)
The NumPy version used in this article is as follows. Note that functionality may vary between versions. For example, consider reading the following CSV file, which contains missing data, using `np.genfromtxt()`.
- [sample\_nan.csv](https://raw.githubusercontent.com/nkmk/python-snippets/8cd2941cdad298679b8afc22dff07daa0fc1eae2/notebook/data/src/sample_nan.csv)
- [NumPy: Read and write CSV files (np.loadtxt, np.genfromtxt, np.savetxt)](https://note.nkmk.me/en/python-numpy-loadtxt-genfromtxt-savetxt/)
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L1-L10)
## Remove all `NaN` from an array
You can use `np.isnan()` to check if values in an `ndarray` are `NaN`.
- [numpy.isnan â NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.isnan.html)
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L6-L15)
Applying the negation operator (`~`) to this resulting `ndarray` turns `NaN` to `False`, which can be used as a mask to remove `NaN` (extract non-NaN values). Since the number of remaining elements changes, the resulting `ndarray` does not retain the same shape as the original `ndarray`, but instead becomes flattened (converted to one-dimensional).
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L17-L23)
## Remove rows containing `NaN`
To remove rows containing `NaN`, call the `any()` method on the `ndarray` generated by `np.isnan()`. The `any()` method returns `True` if there is at least one `True` in the `ndarray`.
- [numpy.ndarray.any â NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.any.html)
By setting `axis=1` in `any()`, it checks whether there is at least one `True` in each row, indicating the presence of `NaN`.
- [NumPy: Meaning of the axis parameter (0, 1, -1)](https://note.nkmk.me/en/python-numpy-axis-keepdims/)
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L25-L26)
Using the negation operator (`~`) to swap `True` and `False`, rows without any `NaN` become `True`.
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L28-L29)
By applying this `ndarray` to the rows (the first dimension) of the original `ndarray`, you can remove rows with `NaN` (extract rows without `NaN`).
- [NumPy: Get and set values in an array using various indexing](https://note.nkmk.me/en/python-numpy-select-element-row-column-array/)
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L31-L32)
You can omit the column specification (`:`) as shown below.
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L34-L35)
To remove only rows where all elements are `NaN`, use `all()` instead of `any()`.
- [numpy.ndarray.all â NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.all.html)
Setting `axis=1` checks if all elements in each row are `True`. Here, `np.nan` is assigned to elements for explanation.
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L37-L52)
## Remove columns containing `NaN`
The process to remove columns containing `NaN` is similar to that used for rows.
Using `any()` with `axis=0` checks if there is at least one `True` in each column, indicating the presence of `NaN`. Apply the negation operator (`~`) to convert columns without any `NaN` to `True`.
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L54-L69)
By applying this `ndarray` to the columns (the second dimension) of the original `ndarray`, you can remove columns with `NaN` (extract columns without `NaN`).
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L71-L74)
To remove only columns where all elements are `NaN`, use `all()` instead of `any()`.
```
```
source: [numpy\_nan\_remove.py](https://github.com/nkmk/python-snippets/blob/2a4597b0986276a982467fa59457e61cc8b88054/notebook/numpy_nan_remove.py#L76-L91)
## Related Categories
- [Python](https://note.nkmk.me/en/python/)
- [NumPy](https://note.nkmk.me/en/numpy/)
## Related Articles
- [NumPy: reshape() to change the shape of an array](https://note.nkmk.me/en/python-numpy-reshape-usage/)
- [NumPy: Flip array (np.flip, flipud, fliplr)](https://note.nkmk.me/en/python-numpy-flip-flipud-fliplr/)
- [Image processing with Python, NumPy](https://note.nkmk.me/en/python-numpy-image-processing/)
- [NumPy: Generate random numbers with np.random](https://note.nkmk.me/en/python-numpy-random/)
- [NumPy: Flatten an array with ravel() and flatten()](https://note.nkmk.me/en/python-numpy-ravel-flatten/)
- [NumPy: Set the display format for ndarray](https://note.nkmk.me/en/python-numpy-set-printoptions-float-formatter/)
- [How to fix "ValueError: The truth value ... is ambiguous" in NumPy, pandas](https://note.nkmk.me/en/python-numpy-pandas-value-error-ambiguous/)
- [NumPy: arange() and linspace() to generate evenly spaced values](https://note.nkmk.me/en/python-numpy-arange-linspace/)
- [NumPy: Create an array with the same value (np.zeros, np.ones, np.full)](https://note.nkmk.me/en/python-numpy-zeros-ones-full/)
- [NumPy: Read and write CSV files (np.loadtxt, np.genfromtxt, np.savetxt)](https://note.nkmk.me/en/python-numpy-loadtxt-genfromtxt-savetxt/)
- [NumPy: Delete rows/columns from an array with np.delete()](https://note.nkmk.me/en/python-numpy-delete/)
- [NumPy: Broadcasting rules and examples](https://note.nkmk.me/en/python-numpy-broadcasting/)
- [NumPy: np.sign(), np.signbit(), np.copysign()](https://note.nkmk.me/en/python-numpy-sign-signbit-copysign/)
- [NumPy: Calculate cumulative sum and product (np.cumsum, np.cumprod)](https://note.nkmk.me/en/python-numpy-cumsum-cumprod/)
- [NumPy: append() to add values to an array](https://note.nkmk.me/en/python-numpy-append/)
Search
Categories
- [Python](https://note.nkmk.me/en/python/)
- [NumPy](https://note.nkmk.me/en/numpy/)
- [OpenCV](https://note.nkmk.me/en/opencv/)
- [pandas](https://note.nkmk.me/en/pandas/)
- [Pillow](https://note.nkmk.me/en/pillow/)
- [pip](https://note.nkmk.me/en/pip/)
- [scikit-image](https://note.nkmk.me/en/scikit-image/)
- [uv](https://note.nkmk.me/en/uv/)
- [Git](https://note.nkmk.me/en/git/)
- [Jupyter Notebook](https://note.nkmk.me/en/jupyter-notebook/)
- [Mac](https://note.nkmk.me/en/mac/)
- [Windows](https://note.nkmk.me/en/windows/)
- [Image Processing](https://note.nkmk.me/en/image-processing/)
- [File](https://note.nkmk.me/en/file/)
- [CSV](https://note.nkmk.me/en/csv/)
- [JSON](https://note.nkmk.me/en/json/)
- [PDF](https://note.nkmk.me/en/pdf/)
- [Date and time](https://note.nkmk.me/en/date-and-time/)
- [String](https://note.nkmk.me/en/string/)
- [Regex](https://note.nkmk.me/en/regex/)
- [Numeric](https://note.nkmk.me/en/numeric/)
- [Dictionary](https://note.nkmk.me/en/dictionary/)
- [List](https://note.nkmk.me/en/list/)
- [Error handling](https://note.nkmk.me/en/error-handling/)
- [Mathematics](https://note.nkmk.me/en/mathematics/)
- [Summary](https://note.nkmk.me/en/summary/)
About
- GitHub: [nkmk](https://github.com/nkmk)
Related Articles
- [NumPy: reshape() to change the shape of an array](https://note.nkmk.me/en/python-numpy-reshape-usage/)
- [NumPy: Flip array (np.flip, flipud, fliplr)](https://note.nkmk.me/en/python-numpy-flip-flipud-fliplr/)
- [Image processing with Python, NumPy](https://note.nkmk.me/en/python-numpy-image-processing/)
- [NumPy: Generate random numbers with np.random](https://note.nkmk.me/en/python-numpy-random/)
- [NumPy: Flatten an array with ravel() and flatten()](https://note.nkmk.me/en/python-numpy-ravel-flatten/)
- English / [Japanese](https://note.nkmk.me/)
- \|
- [Disclaimer](https://note.nkmk.me/en/disclaimer/)
- [Privacy policy](https://note.nkmk.me/en/privacy-policy/)
- [GitHub](https://github.com/nkmk)
- Š[nkmk.me](https://nkmk.me/) |
| Readable Markdown | In NumPy, to remove rows or columns containing `NaN` (`np.nan`) from an array (`ndarray`), use `np.isnan()` to identify `NaN` and methods like `any()` or `all()` to extract rows or columns that do not contain `NaN`.
Additionally, you can remove all `NaN` values from an array, but this will flatten the array.
Contents
- [Remove all `NaN` from an array](https://note.nkmk.me/en/python-numpy-nan-remove/#remove-all-nan-from-an-array)
- [Remove rows containing `NaN`](https://note.nkmk.me/en/python-numpy-nan-remove/#remove-rows-containing-nan)
- [Remove columns containing `NaN`](https://note.nkmk.me/en/python-numpy-nan-remove/#remove-columns-containing-nan)
For basics on handling `NaN` in Python, refer to the following article.
- [What is nan in Python (float('nan'), math.nan, np.nan)](https://note.nkmk.me/en/python-nan-usage/)
For replacing `NaN` with other values instead of removing them, refer to the following article.
- [NumPy: Replace NaN (np.nan) using np.nan\_to\_num() and np.isnan()](https://note.nkmk.me/en/python-numpy-nan-replace/)
The NumPy version used in this article is as follows. Note that functionality may vary between versions. For example, consider reading the following CSV file, which contains missing data, using `np.genfromtxt()`.
- [sample\_nan.csv](https://raw.githubusercontent.com/nkmk/python-snippets/8cd2941cdad298679b8afc22dff07daa0fc1eae2/notebook/data/src/sample_nan.csv)
- [NumPy: Read and write CSV files (np.loadtxt, np.genfromtxt, np.savetxt)](https://note.nkmk.me/en/python-numpy-loadtxt-genfromtxt-savetxt/)
```
```
## Remove all `NaN` from an array
You can use `np.isnan()` to check if values in an `ndarray` are `NaN`.
- [numpy.isnan â NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.isnan.html)
```
```
Applying the negation operator (`~`) to this resulting `ndarray` turns `NaN` to `False`, which can be used as a mask to remove `NaN` (extract non-NaN values). Since the number of remaining elements changes, the resulting `ndarray` does not retain the same shape as the original `ndarray`, but instead becomes flattened (converted to one-dimensional).
```
```
## Remove rows containing `NaN`
To remove rows containing `NaN`, call the `any()` method on the `ndarray` generated by `np.isnan()`. The `any()` method returns `True` if there is at least one `True` in the `ndarray`.
- [numpy.ndarray.any â NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.any.html)
By setting `axis=1` in `any()`, it checks whether there is at least one `True` in each row, indicating the presence of `NaN`.
- [NumPy: Meaning of the axis parameter (0, 1, -1)](https://note.nkmk.me/en/python-numpy-axis-keepdims/)
```
```
Using the negation operator (`~`) to swap `True` and `False`, rows without any `NaN` become `True`.
```
```
By applying this `ndarray` to the rows (the first dimension) of the original `ndarray`, you can remove rows with `NaN` (extract rows without `NaN`).
- [NumPy: Get and set values in an array using various indexing](https://note.nkmk.me/en/python-numpy-select-element-row-column-array/)
```
```
You can omit the column specification (`:`) as shown below.
```
```
To remove only rows where all elements are `NaN`, use `all()` instead of `any()`.
- [numpy.ndarray.all â NumPy v1.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.all.html)
Setting `axis=1` checks if all elements in each row are `True`. Here, `np.nan` is assigned to elements for explanation.
```
```
## Remove columns containing `NaN`
The process to remove columns containing `NaN` is similar to that used for rows.
Using `any()` with `axis=0` checks if there is at least one `True` in each column, indicating the presence of `NaN`. Apply the negation operator (`~`) to convert columns without any `NaN` to `True`.
```
```
By applying this `ndarray` to the columns (the second dimension) of the original `ndarray`, you can remove columns with `NaN` (extract columns without `NaN`).
```
```
To remove only columns where all elements are `NaN`, use `all()` instead of `any()`.
```
``` |
| Shard | 13 (laksa) |
| Root Hash | 14415757146955323613 |
| Unparsed URL | me,nkmk!note,/en/python-numpy-nan-remove/ s443 |