ℹ️ 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.2 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.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array |
| Last Crawled | 2026-04-08 04:19:11 (6 days ago) |
| First Indexed | 2022-01-09 16:21:50 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Removing rows containing NaN in a NumPy array |
| Meta Description | To remove rows containing NaN in a NumPy array, we can use a combination of the isnan(~) and any(~) methods. |
| Meta Canonical | null |
| Boilerpipe Text | To remove rows containing
NaN
in a NumPy array, we can use a combination of the
isnan(~)
and
any(~)
methods.
Example
Consider the following array:
np.
array
([[
1
,
2
,np.
nan
], [
4
,
5
,
6
]])
array([[ 1., 2., nan],
[ 4., 5., 6.]])
To remove rows containing
NaN
:
a = np.
array
([[
1
,
2
,np.
nan
], [
4
,
5
,
6
]])
a[~np.
isnan
(a).
any
(
axis=
1
)]
array([[4., 5., 6.]])
Explanation
In the above code snippet, first we are checking each element in array
a
for
np.nan
using
isnan(~)
:
np.
isnan
(a)
array([[False, False, True],
[False, False, False]])
Next
any(axis=1)
returns
True
if at least one element in each row evaluates to
True
:
np.
isnan
(a).
any
(
axis=
1
)
array([ True, False])
Finally, the bitwise NOT (
~
) operator inverts the
True
and
False
values:
~np.
isnan
(a).
any
(
axis=
1
)
array([False, True])
Now we apply this boolean mask to the original array
a
to return only the rows not containing
NaN
:
a[~np.
isnan
(a).
any
(
axis=
1
)]
array([[4., 5., 6.]]) |
| Markdown | menu
[home About](https://www.skytowner.com/)
[store Store](https://www.skytowner.com/shop)
[paid Pricing](https://www.skytowner.com/#pricing)
login
Log in
[team\_dashboard Dashboard](https://www.skytowner.com/app)
[near\_me **Linear Algebra**](https://www.skytowner.com/explore/introduction_to_vectors_in_linear_algebra)
[casino **Prob and Stats**](https://www.skytowner.com/explore/population_samples_and_sampling_techniques)
[function **Other math topics**](https://www.skytowner.com/explore/introduction_to_complex_numbers)
[smart\_toy **Machine Learning**](https://www.skytowner.com/explore/introduction_to_linear_regression)
[ **Pandas**](https://www.skytowner.com/explore/getting_started_with_pandas)
[ **NumPy**](https://www.skytowner.com/explore/getting_started_with_numpy)
[ **Matplotlib**](https://www.skytowner.com/explore/getting_started_with_matplotlib)
[ **PySpark**](https://www.skytowner.com/explore/getting_started_with_pyspark)
[ **MySQL**](https://www.skytowner.com/explore/getting_started_with_mysql)
[ **Dagster (NEW)**](https://www.skytowner.com/explore/getting_started_with_dagster)
[SkyTowner BETA](https://www.skytowner.com/)
*search*
Search
Login
[Unlock 100+ guides](https://www.skytowner.com/)
![]()
menu
*menu*
[web](https://www.skytowner.com/app)
[SkyTowner](https://www.skytowner.com/)
*search* *toc*
*close*
[history\_edu Blog](https://www.skytowner.com/blog)
[help Help](https://www.skytowner.com/help)
Outline
[Example](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array#example)[Explanation](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array#example/explanation)
Comments
Log in or [sign up](https://www.skytowner.com/signup)
Cancel
Post
[*account\_circle* Profile]()
*exit\_to\_app*
Sign out
What does this mean?
Why is this true?
Give me some examples\!
search
keyboard\_voice
close
**Searching Tips**
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "\#python"
**Useful Shortcuts**
/ to open search panel
Esc to close search panel
↑↓ to navigate between search results
⌘d to clear all current filters
⌘Enter to expand content preview

Doc Search

Code Search Beta

SORRY NOTHING FOUND\!
*mic*
Start speaking...

Voice search is only supported in Safari and Chrome.
[fullscreen\_exit]()
Shrink
[north\_east]()
Navigate to

**NumPy**
319 guides
keyboard\_arrow\_down
[near\_me **Linear Algebra** 54 guides](https://www.skytowner.com/explore/introduction_to_vectors_in_linear_algebra)
[casino **Prob and Stats** 38 guides](https://www.skytowner.com/explore/population_samples_and_sampling_techniques)
[smart\_toy **Machine Learning** 36 guides](https://www.skytowner.com/explore/introduction_to_linear_regression)
[function **Other math topics** 10 guides](https://www.skytowner.com/explore/introduction_to_complex_numbers)
[ **Dagster** 11 guides](https://www.skytowner.com/explore/getting_started_with_dagster)
[ **Pandas** 655 guides](https://www.skytowner.com/explore/getting_started_with_pandas)
[ **NumPy** 319 guides](https://www.skytowner.com/explore/getting_started_with_numpy)
[ **Matplotlib** 83 guides](https://www.skytowner.com/explore/getting_started_with_matplotlib)
[ **PySpark** 147 guides](https://www.skytowner.com/explore/getting_started_with_pyspark)
[ **MySQL** 295 guides](https://www.skytowner.com/explore/getting_started_with_mysql)
*chevron\_left***Cookbooks**
[Accessing a value in a 2D array](https://www.skytowner.com/explore/accessing_a_value_in_a_numpy_2d_array)[Accessing columns of a 2D array](https://www.skytowner.com/explore/accessing_columns_of_a_2d_numpy_array)[Accessing rows of a 2D array](https://www.skytowner.com/explore/accessing_rows_of_a_2d_numpy_array)[Calculating the determinant of a matrix](https://www.skytowner.com/explore/calculating_the_determinant_of_a_matrix_in_python)[Checking allowed values for a NumPy data type](https://www.skytowner.com/explore/checking_allowed_values_for_a_numpy_data_type)[Checking if a NumPy array is a view or copy](https://www.skytowner.com/explore/checking_if_a_numpy_array_is_a_view_or_copy)[Checking the version of NumPy](https://www.skytowner.com/explore/checking_the_version_of_numpy)[Checking whether a NumPy array contains a given row](https://www.skytowner.com/explore/checking_whether_a_numpy_array_contains_a_given_row)[Computing Euclidean distance using Numpy](https://www.skytowner.com/explore/computing_euclidean_distance_using_numpy)[Concatenating 1D arrays](https://www.skytowner.com/explore/concatenating_1d_numpy_arrays)[Converting array to lowercase](https://www.skytowner.com/explore/converting_numpy_array_to_lowercase)[Converting type of NumPy array to string](https://www.skytowner.com/explore/converting_type_of_numpy_array_to_string)[Creating a copy of an array](https://www.skytowner.com/explore/creating_a_copy_of_a_numpy_array)[Difference between Python List and Numpy array](https://www.skytowner.com/explore/difference_between_python_list_and_numpy_array)[Difference between the methods array\_equal and array\_equiv](https://www.skytowner.com/explore/difference_between_the_methods_array_equal_and_array_equiv_in_numpy)[Difference between the methods mod and fmod](https://www.skytowner.com/explore/difference_between_the_methods_mod_and_fmod_in_numpy)[Difference between the methods power and float\_power](https://www.skytowner.com/explore/difference_between_the_methods_power_and_float_power_in_numpy)[Finding the closest value in an array](https://www.skytowner.com/explore/finding_the_closest_value_in_a_numpy_array)[Finding the Index of Largest Value in a Numpy Array](https://www.skytowner.com/explore/finding_the_index_of_largest_value_in_a_numpy_array)[Finding the Index of Smallest Value in a Numpy Array](https://www.skytowner.com/explore/finding_the_index_of_smallest_value_in_a_numpy_array)[Finding the most frequent value in a NumPy array](https://www.skytowner.com/explore/finding_the_most_frequent_value_in_a_numpy_array)[Flattening Numpy arrays](https://www.skytowner.com/explore/flattening_numpy_arrays)[Getting constant Pi](https://www.skytowner.com/explore/getting_constant_pi_in_numpy)[Getting elements from a two dimensional array using two dimensional array of indices](https://www.skytowner.com/explore/getting_elements_from_a_two_dimensional_array_using_two_dimensional_array_of_indices_in_numpy)[Getting indices of N maximum values](https://www.skytowner.com/explore/getting_indices_of_n_maximum_values_in_numpy)[Getting indices of N minimum values](https://www.skytowner.com/explore/getting_indices_of_n_minimum_values_in_numpy)[Getting the number of columns of a 2D array](https://www.skytowner.com/explore/getting_the_number_of_columns_of_a_2d_numpy_array)[Getting the number of non-zero elements in a NumPy array](https://www.skytowner.com/explore/getting_the_number_of_non_zero_elements_in_a_numpy_array)[Getting the number of rows of a 2D array](https://www.skytowner.com/explore/getting_the_number_of_rows_of_a_2d_numpy_array)[Initializing an array of ones](https://www.skytowner.com/explore/initializing_a_numpy_array_of_ones)[Initializing an array of zeros](https://www.skytowner.com/explore/initializing_a_numpy_array_of_zeros)[Initializing an identity matrix](https://www.skytowner.com/explore/initializing_an_identity_matrix_in_numpy)[Limiting array values to a certain range](https://www.skytowner.com/explore/limiting_array_values_to_a_certain_range_in_numpy)[Performing linear regression](https://www.skytowner.com/explore/performing_linear_regression_in_numpy)[Printing full or truncated NumPy array](https://www.skytowner.com/explore/printing_full_or_truncated_numpy_array)[Printing large Numpy arrays without truncation](https://www.skytowner.com/explore/printing_large_numpy_arrays_without_truncation)[Removing rows containing NaN in a NumPy array](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array)[Reversing a NumPy array](https://www.skytowner.com/explore/reversing_a_numpy_array)[Saving NumPy array to a file](https://www.skytowner.com/explore/saving_numpy_array_to_a_file)[Shape of Numpy Arrays](https://www.skytowner.com/explore/shape_of_numpy_arrays)[Sorting value of one array according to another](https://www.skytowner.com/explore/sorting_value_of_one_array_according_to_another_in_python)[Suppressing scientific notation](https://www.skytowner.com/explore/suppressing_scientific_notation_when_printing_numpy_arrays)
check\_circle
Mark as learned
thumb\_up
1
thumb\_down
0
chat\_bubble\_outline
0
Comment
auto\_stories Bi-column layout
settings
# Removing rows containing NaN in a NumPy array
*schedule* Aug 10, 2023
Last updated
local\_offer
Python●NumPy
Tags
*toc*Table of Contents
*expand\_more*
[Example](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array#example)[Explanation](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array#example/explanation)
[mode\_heat Master the **mathematics behind data science** with 100+ top-tier guides Start your free 7-days trial now\!](https://www.skytowner.com/)
To remove rows containing `NaN` in a NumPy array, we can use a combination of the [`isnan(~)`](https://www.skytowner.com/explore/numpy_isnan_method) and [`any(~)`](https://www.skytowner.com/explore/numpy_any_method) methods.
# Example
Consider the following array:
```
np.array([[1,2,np.nan], [4,5,6]]) array([[ 1., 2., nan], [ 4., 5., 6.]])
```
To remove rows containing `NaN`:
```
a = np.array([[1,2,np.nan], [4,5,6]]) a[~np.isnan(a).any(axis=1)] array([[4., 5., 6.]])
```
## Explanation
In the above code snippet, first we are checking each element in array `a` for `np.nan` using `isnan(~)`:
```
np.isnan(a) array([[False, False, True], [False, False, False]])
```
Next `any(axis=1)` returns `True` if at least one element in each row evaluates to `True`:
```
np.isnan(a).any(axis=1) array([ True, False])
```
Finally, the bitwise NOT (`~`) operator inverts the `True` and `False` values:
```
~np.isnan(a).any(axis=1) array([False, True])
```
Now we apply this boolean mask to the original array `a` to return only the rows not containing `NaN`:
```
a[~np.isnan(a).any(axis=1)] array([[4., 5., 6.]])
```
[](https://www.skytowner.com/profile/jo42E3w6Unbqe8vKejJonz4GTtx1)
Published by [Arthur Yanagisawa](https://www.skytowner.com/profile/jo42E3w6Unbqe8vKejJonz4GTtx1)
Edited by 0 others
Did you find this page useful?
thumb\_up
thumb\_down
Comment
Citation
Ask a question or leave a feedback...
Outline
[Example](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array#example)[Explanation](https://www.skytowner.com/explore/removing_rows_containing_nan_in_a_numpy_array#example/explanation)
thumb\_up
1
thumb\_down
0
chat\_bubble\_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes\!
Navigation
[Home](https://www.skytowner.com/) [Blog](https://www.skytowner.com/blog)
Contact us
Resources
[Python](https://www.skytowner.com/explore/getting_started_with_python) [Pandas](https://www.skytowner.com/explore/getting_started_with_pandas) [MySQL](https://www.skytowner.com/explore/getting_started_with_mysql) [Beautiful Soup](https://www.skytowner.com/explore/getting_started_with_beautiful_soup) [Matplotlib](https://www.skytowner.com/explore/getting_started_with_matplotlib) [NumPy](https://www.skytowner.com/explore/getting_started_with_numpy) [PySpark](https://www.skytowner.com/explore/user_guide_pyspark)
Community
[ Join our Discord](https://discord.gg/fwy7emdn9Y)
[mail Join our newsletter for updates on new comprehensive DS/ML guides](https://www.skytowner.com/signup)
[Terms](https://www.skytowner.com/terms)
\|
[Privacy](https://www.skytowner.com/privacy) |
| Readable Markdown | To remove rows containing `NaN` in a NumPy array, we can use a combination of the [`isnan(~)`](https://www.skytowner.com/explore/numpy_isnan_method) and [`any(~)`](https://www.skytowner.com/explore/numpy_any_method) methods.
## Example
Consider the following array:
To remove rows containing `NaN`:
## Explanation
In the above code snippet, first we are checking each element in array `a` for `np.nan` using `isnan(~)`:
Next `any(axis=1)` returns `True` if at least one element in each row evaluates to `True`:
Finally, the bitwise NOT (`~`) operator inverts the `True` and `False` values:
Now we apply this boolean mask to the original array `a` to return only the rows not containing `NaN`: |
| Shard | 121 (laksa) |
| Root Hash | 9074883843937675921 |
| Unparsed URL | com,skytowner!www,/explore/removing_rows_containing_nan_in_a_numpy_array s443 |