ℹ️ 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 | 1 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.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php |
| Last Crawled | 2026-03-13 19:26:45 (1 month ago) |
| First Indexed | 2019-06-26 03:49:45 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | NumPy: Remove nan values from a given array - w3resource |
| Meta Description | NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to remove nan values from a given array. |
| Meta Canonical | null |
| Boilerpipe Text | Last update on August 29 2025 12:45:45 (UTC/GMT +8 hours)
Remove nan Values from Array
Write a NumPy program to remove nan values from a given array.
Sample Solution
:
Python Code:
# Importing the NumPy library and aliasing it as 'np'
import numpy as np
# Creating a NumPy array 'x' containing integers and 'np.nan' (representing missing values)
x = np.array([200, 300, np.nan, np.nan, np.nan, 700])
# Creating a 2D NumPy array 'y' containing integers and 'np.nan' (representing missing values)
y = np.array([[1, 2, 3], [np.nan, 0, np.nan], [6, 7, np.nan]])
# Printing the original array 'x'
print("Original array:")
print(x)
# Removing 'np.nan' values from array 'x' and storing the result in 'result'
result = x[np.logical_not(np.isnan(x))]
# Printing the array 'x' after removing 'np.nan' values
print("After removing nan values:")
print(result)
# Printing a new line
print("\nOriginal array:")
# Printing the original array 'y'
print(y)
# Removing 'np.nan' values from array 'y' and storing the result in 'result'
result = y[np.logical_not(np.isnan(y))]
# Printing the array 'y' after removing 'np.nan' values
print("After removing nan values:")
print(result)
Sample Output:
Original array:
[200. 300. nan nan nan 700.]
After removing nan values:
[200. 300. 700.]
Original array:
[[ 1. 2. 3.]
[nan 0. nan]
[ 6. 7. nan]]
After removing nan values:
[1. 2. 3. 0. 6. 7.]
Explanation:
The above code shows how to remove NaN (Not-a-Number) elements from two NumPy arrays: a 1D array 'x' and a 2D array 'y'.
x = np.array([200, 300, np.nan, np.nan, np.nan ,700]): Create a 1D NumPy array 'x' containing some numbers and NaN values.
y = np.array([[1, 2, 3], [np.nan, 0, np.nan] ,[6,7,np.nan]]): Create a 2D NumPy array 'y' containing some numbers and NaN values.
result = x[np.logical_not(np.isnan(x))]: Use np.isnan(x) to create a boolean array where 'True' represents a NaN value in 'x'. Then, use np.logical_not to invert this boolean array. Finally, use boolean indexing to extract elements from 'x' where the corresponding value in the inverted boolean array is 'True' (i.e., not NaN). Store the result in the variable 'result'.
result = y[np.logical_not(np.isnan(y))]: Repeat the same process for the 2D array 'y'. Note that 'y' is flattened before removing NaN values, so the resulting 'result' variable will be a 1D array.
Pictorial Presentation:
For more Practice: Solve these Related Problems:
Write a NumPy program to remove nan values from a 1D array using boolean indexing and np.isnan.
Create a function that flattens a 2D array, removes all nan entries, and returns a clean 1D array.
Test the removal process on an array with mixed nan placements and verify that only valid numbers remain.
Implement a solution that replaces nan values with the mean of the non-nan elements instead of removing them.
Go to:
NumPy Array Exercises Home ↩
NumPy Exercises Home ↩
PREV :
Convert NumPy Array to Image
NEXT :
Create Cartesian Product of Two Arrays
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus. |
| Markdown | **
[w3resource](https://www.w3resource.com/index.php)
Web
Image
| | |
|---|---|
| | Sort by Relevance Date |

[*home*](https://www.w3resource.com/index.php) [Front End](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [HTML](https://www.w3resource.com/html/HTML-tutorials.php) [CSS](https://www.w3resource.com/css/CSS-tutorials.php) [JavaScript](https://www.w3resource.com/javascript/javascript.php) [HTML5](https://www.w3resource.com/html5/introduction.php) [Schema.org](https://www.w3resource.com/schema.org/introduction.php) [php.js](https://www.w3resource.com/phpjs/use-php-functions-in-javascript.php) [Twitter Bootstrap](https://www.w3resource.com/twitter-bootstrap/tutorial.php) [Responsive Web Design tutorial](https://www.w3resource.com/responsive-web-design/overview.php) [Zurb Foundation 3 tutorials](https://www.w3resource.com/zurb-foundation3/introduction.php) [Pure CSS](https://www.w3resource.com/pure/) [HTML5 Canvas](https://www.w3resource.com/html5-canvas/) [JavaScript Course](https://www.w3resource.com/course/javascript-course.html) [Icon](https://www.w3resource.com/icon/) [Angular](https://www.w3resource.com/angular/getting-started-with-angular.php) [Vue](https://www.w3resource.com/vue/installation.php) [Jest](https://www.w3resource.com/jest/introduction.php) [Mocha](https://www.w3resource.com/mocha/getting-started.php) [NPM](https://www.w3resource.com/npm/getting-started-with-npm.php) [Yarn](https://www.w3resource.com/yarn/getting-started-with-yarn.php) [Back End](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [PHP](https://www.w3resource.com/php/php-home.php) [Python](https://www.w3resource.com/python/python-tutorial.php) [Java](https://www.w3resource.com/java-tutorial/) [Node.js](https://www.w3resource.com/node.js/node.js-tutorials.php) [Ruby](https://www.w3resource.com/ruby/) [C programming](https://www.w3resource.com/c-programming/programming-in-c.php) [PHP Composer](https://www.w3resource.com/php/composer/a-gentle-introduction-to-composer.php) [Laravel](https://www.w3resource.com/laravel/laravel-tutorial.php) [PHPUnit](https://www.w3resource.com/php/PHPUnit/a-gentle-introduction-to-unit-test-and-testing.php) [Database](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [SQL(2003 standard of ANSI)](https://www.w3resource.com/sql/tutorials.php) [MySQL](https://www.w3resource.com/mysql/mysql-tutorials.php) [PostgreSQL](https://www.w3resource.com/PostgreSQL/tutorial.php) [SQLite](https://www.w3resource.com/sqlite/) [NoSQL](https://www.w3resource.com/mongodb/nosql.php) [MongoDB](https://www.w3resource.com/mongodb/nosql.php) [Oracle](https://www.w3resource.com/oracle/index.php) [Redis](https://www.w3resource.com/redis/index.php) [Apollo GraphQL](https://www.w3resource.com/apollo-graphql/the-apollo-graphql-platform.php) [API](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [Google Plus API](https://www.w3resource.com/API/google-plus/tutorial.php) [Youtube API](https://www.w3resource.com/API/youtube/tutorial.php) [Google Maps API](https://www.w3resource.com/API/google-maps/index.php) [Flickr API](https://www.w3resource.com/API/flickr/tutorial.php) [Last.fm API](https://www.w3resource.com/API/last.fm/tutorial.php) [Twitter REST API](https://www.w3resource.com/API/twitter-rest-api/) [Data Interchnage](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [XML](https://www.w3resource.com/xml/xml.php) [JSON](https://www.w3resource.com/JSON/introduction.php) [Ajax](https://www.w3resource.com/ajax/introduction.php) [Exercises](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [HTML CSS Exercises](https://www.w3resource.com/html-css-exercise/index.php) [JavaScript Exercises](https://www.w3resource.com/javascript-exercises/) [jQuery Exercises](https://www.w3resource.com/jquery-exercises/) [jQuery-UI Exercises](https://www.w3resource.com/jquery-ui-exercises/) [CoffeeScript Exercises](https://www.w3resource.com/coffeescript-exercises/) [PHP Exercises](https://www.w3resource.com/php-exercises/) [Python Exercises](https://www.w3resource.com/python-exercises/) [C Programming Exercises](https://www.w3resource.com/c-programming-exercises/) [C\# Sharp Exercises](https://www.w3resource.com/csharp-exercises/) [Java Exercises](https://www.w3resource.com/java-exercises/) [SQL Exercises](https://www.w3resource.com/sql-exercises/) [Oracle Exercises](https://www.w3resource.com/oracle-exercises/) [MySQL Exercises](https://www.w3resource.com/mysql-exercises/) [SQLite Exercises](https://www.w3resource.com/sqlite-exercises/) [PostgreSQL Exercises](https://www.w3resource.com/postgresql-exercises/) [MongoDB Exercises](https://www.w3resource.com/mongodb-exercises/) [Twitter Bootstrap Examples](https://www.w3resource.com/twitter-bootstrap/examples.php) [Others](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php) [Excel Tutorials](https://www.w3resource.com/excel/) [Useful tools](https://www.w3resource.com/web-development-tools/useful-web-development-tools.php) [Google Docs Forms Templates](https://www.w3resource.com/form-template/) [Google Docs Slide Presentations](https://www.w3resource.com/slides/) [Number Conversions](https://www.w3resource.com/convert/number/binary-to-decimal.php) [Linux Tutorials](https://www.w3resource.com/linux-system-administration/installation.php) [Quizzes](https://www.w3resource.com/quizzes/python/index.php) [Articles](https://www.w3resource.com/Articles/index.php)
# NumPy: Remove nan values from a given array
Last update on August 29 2025 12:45:45 (UTC/GMT +8 hours)
***
Remove nan Values from Array
Write a NumPy program to remove nan values from a given array.
**Sample Solution**:
**Python Code:**
```
```
Sample Output:
```
Original array:
[200. 300. nan nan nan 700.]
After removing nan values:
[200. 300. 700.]
Original array:
[[ 1. 2. 3.]
[nan 0. nan]
[ 6. 7. nan]]
After removing nan values:
[1. 2. 3. 0. 6. 7.]
```
**Explanation:**
The above code shows how to remove NaN (Not-a-Number) elements from two NumPy arrays: a 1D array 'x' and a 2D array 'y'.
x = np.array(\[200, 300, np.nan, np.nan, np.nan ,700\]): Create a 1D NumPy array 'x' containing some numbers and NaN values.
y = np.array(\[\[1, 2, 3\], \[np.nan, 0, np.nan\] ,\[6,7,np.nan\]\]): Create a 2D NumPy array 'y' containing some numbers and NaN values.
result = x\[np.logical\_not(np.isnan(x))\]: Use np.isnan(x) to create a boolean array where 'True' represents a NaN value in 'x'. Then, use np.logical\_not to invert this boolean array. Finally, use boolean indexing to extract elements from 'x' where the corresponding value in the inverted boolean array is 'True' (i.e., not NaN). Store the result in the variable 'result'.
result = y\[np.logical\_not(np.isnan(y))\]: Repeat the same process for the 2D array 'y'. Note that 'y' is flattened before removing NaN values, so the resulting 'result' variable will be a 1D array.
**Pictorial Presentation:**

For more Practice: Solve these Related Problems:
- Write a NumPy program to remove nan values from a 1D array using boolean indexing and np.isnan.
- Create a function that flattens a 2D array, removes all nan entries, and returns a clean 1D array.
- Test the removal process on an array with mixed nan placements and verify that only valid numbers remain.
- Implement a solution that replaces nan values with the mean of the non-nan elements instead of removing them.
Go to:
- [NumPy Array Exercises Home ↩](https://www.w3resource.com/python-exercises/numpy/index-array.php)
- [NumPy Exercises Home ↩](https://www.w3resource.com/python-exercises/numpy/index.php)
**PREV :** [Convert NumPy Array to Image](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-109.php)
**NEXT :** [Create Cartesian Product of Two Arrays](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-111.php)
**Python-Numpy Code Editor:**
**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**
**What is the difficulty level of this exercise?**
Test your Programming skills with w3resource's [quiz.](https://www.w3resource.com/quizzes/index.php)
Follow us on [Facebook](https://www.facebook.com/W3resource-103553425799800) and [Twitter](https://twitter.com/w3resource) for latest update.
***
- [Daily Coding Challenges & Projects](https://www.w3resource.com/daily-learning/daily-learning.php)
- [Weekly Trends and Language Statistics](https://www.w3resource.com/weekly-trends-and-language-statistics.php)
***
***
Load Disqus Comments
***
[This work is licensed under a Creative Commons Attribution 4.0 International License.](http://creativecommons.org/licenses/by-nc/4.0/)
©w3resource.com 2011-2026
- [Read our Privacy Policy](https://www.w3resource.com/privacy.php)
- [About](https://www.w3resource.com/about.php)
- [Contact](https://www.w3resource.com/contact.php)
- [Feedback](https://www.w3resource.com/feedback.php)
- [Advertise](https://www.w3resource.com/advertise.php)
[**](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-110.php#h_one) |
| Readable Markdown | Last update on August 29 2025 12:45:45 (UTC/GMT +8 hours)
***
Remove nan Values from Array
Write a NumPy program to remove nan values from a given array.
**Sample Solution**:
**Python Code:**
```
```
Sample Output:
```
Original array:
[200. 300. nan nan nan 700.]
After removing nan values:
[200. 300. 700.]
Original array:
[[ 1. 2. 3.]
[nan 0. nan]
[ 6. 7. nan]]
After removing nan values:
[1. 2. 3. 0. 6. 7.]
```
**Explanation:**
The above code shows how to remove NaN (Not-a-Number) elements from two NumPy arrays: a 1D array 'x' and a 2D array 'y'.
x = np.array(\[200, 300, np.nan, np.nan, np.nan ,700\]): Create a 1D NumPy array 'x' containing some numbers and NaN values.
y = np.array(\[\[1, 2, 3\], \[np.nan, 0, np.nan\] ,\[6,7,np.nan\]\]): Create a 2D NumPy array 'y' containing some numbers and NaN values.
result = x\[np.logical\_not(np.isnan(x))\]: Use np.isnan(x) to create a boolean array where 'True' represents a NaN value in 'x'. Then, use np.logical\_not to invert this boolean array. Finally, use boolean indexing to extract elements from 'x' where the corresponding value in the inverted boolean array is 'True' (i.e., not NaN). Store the result in the variable 'result'.
result = y\[np.logical\_not(np.isnan(y))\]: Repeat the same process for the 2D array 'y'. Note that 'y' is flattened before removing NaN values, so the resulting 'result' variable will be a 1D array.
**Pictorial Presentation:**

For more Practice: Solve these Related Problems:
- Write a NumPy program to remove nan values from a 1D array using boolean indexing and np.isnan.
- Create a function that flattens a 2D array, removes all nan entries, and returns a clean 1D array.
- Test the removal process on an array with mixed nan placements and verify that only valid numbers remain.
- Implement a solution that replaces nan values with the mean of the non-nan elements instead of removing them.
Go to:
- [NumPy Array Exercises Home ↩](https://www.w3resource.com/python-exercises/numpy/index-array.php)
- [NumPy Exercises Home ↩](https://www.w3resource.com/python-exercises/numpy/index.php)
**PREV :** [Convert NumPy Array to Image](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-109.php)
**NEXT :** [Create Cartesian Product of Two Arrays](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-111.php)
**Python-Numpy Code Editor:**
**Have another way to solve this solution? Contribute your code (and comments) through Disqus.** |
| Shard | 161 (laksa) |
| Root Hash | 10695350316657548561 |
| Unparsed URL | com,w3resource!www,/python-exercises/numpy/python-numpy-exercise-110.php s443 |