How to Remove NaN Values from NumPy Array (3 Methods) - Statology (2024)

You can use the following methods to remove NaN values from a NumPy array:

Method 1: Use isnan()

new_data = data[~np.isnan(data)]

Method 2: Use isfinite()

new_data = data[np.isfinite(data)]

Method 3: Use logical_not()

new_data = data[np.logical_not(np.isnan(data))]

Each of these methods produce the same result, but the first method is the shortest to type out so it tends to be used most often.

The following examples show how to use each method in practice.

Example 1: Remove NaN Values Using isnan()

The following code shows how to remove NaN values from a NumPy array by using the isnan() function:

import numpy as np#create array of datadata = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])#define new array of data with nan values removednew_data = data[~np.isnan(data)]#view new arrayprint(new_data)[ 4. 6. 10. 11. 14. 19. 22.]

Notice that the two NaN values have been successfully removed from the NumPy array.

This method simply keeps all of the elements in the array that are not (~) NaN values.

Example 2: Remove NaN Values Using isfinite()

The following code shows how to remove NaN values from a NumPy array by using the isfinite() function:

import numpy as np#create array of datadata = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])#define new array of data with nan values removednew_data = data[np.isfinite(data)]#view new arrayprint(new_data)[ 4. 6. 10. 11. 14. 19. 22.]

Notice that the two NaN values have been successfully removed from the NumPy array.

This method simply keeps all of the elements in the array that are finite values.

Since NaN values are not finite, they’re removed from the array.

Example 3: Remove NaN Values Using logical_not()

The following code shows how to remove NaN values from a NumPy array by using the logical_not() function:

import numpy as np#create array of datadata = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])#define new array of data with nan values removednew_data = data[np.logical_not(np.isnan(data))]#view new arrayprint(new_data)[ 4. 6. 10. 11. 14. 19. 22.]

Notice that the two NaN values have been successfully removed from the NumPy array.

While this method is equivalent to the previous two, it requires more typing so it’s not used as often.

Additional Resources

The following tutorials explain how to perform other common operations in Python:

Pandas: How to Replace Empty Strings with NaN
Pandas: How to Replace NaN Values with String

How to Remove NaN Values from NumPy Array (3 Methods) - Statology (2024)
Top Articles
Crypto Spot Trading Exchange Comparison! Which is better a place to trade?
The Ascent of Money by Niall Ferguson: 9780143116172 | PenguinRandomHouse.com: Books
Helicopter Over Massapequa Now
Spectrum Store Appointment
Barstool Sports Gif
Sessional Dates U Of T
Britley Ritz - K99.1FM
Promiseb Discontinued
Sir Mo Farah says 'sport saved me' after finishing final race of illustrious career at Great North Run
92801 Sales Tax
Ge Tracker Awakener Orb
Craigslist Furniture By Owner Dallas
Fisher-Cheney Funeral Home Obituaries
Pwc Transparency Report
Employment Vacancies - Find Jobs with our recruitment team
My Big Fat Greek Wedding 3 Showtimes Near Regal Ukiah
9:00 A.m. Cdt
T33N Leak Age 5-17
Albany Want Ad Digest
Christian Hogue co*ck
The Legend of Zelda: Every Reincarnation of Princess Zelda Explained
Frostbite Blaster
Bustime B8
Funny Shooter Unblocked
Milwaukee Nickname Crossword Clue
Nikki Catsouras Head Cut In Half
Operation Fortune Showtimes Near Century Rio 24
O'reilly's Los Banos
Coverwood Terriers For Sale
Antonio Brown Football Reference
Stephen King's The Boogeyman Movie: Release Date, Trailer And Other Things We Know About The Upcoming Adaptation
Jcpenney Salon Salinas
Ftbt Ugly God Lyrics
631 West Skyline Parkway, Duluth, MN 55806 | Compass
Issue November 5, 1949 - The Hockey News
Ebk Jaaybo Net Worth
Associate Resources Aces-How To Create An Account And How Its Features Work
Nahant Magic Seaweed
My Perspectives Grade 10 Volume 1 Answer Key Pdf
Patriot Ledger Obits Today
Craigslist Hawley Pa
Booknet.com Contract Marriage 2
Cvs On 30Th And Fowler
Ap Bio Unit 2 Progress Check Mcq
1 Filmy4Wap In
Heatinghelp The Wall
Hughie Francis Foley
Craigslist Pets Olympia
Rs3 Spectral Spirit Shield
Nine Star Hegemon Body Art
8X10 Meters To Square Meters
Ravenna Greatsword Arcane Odyssey
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6382

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.