Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3997400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:23:50+00:00 2026-05-20T07:23:50+00:00

I have a 2D numpy array. Some of the values in this array are

  • 0

I have a 2D numpy array. Some of the values in this array are NaN. I want to perform certain operations using this array. For example consider the array:

[[   0.   43.   67.    0.   38.]
 [ 100.   86.   96.  100.   94.]
 [  76.   79.   83.   89.   56.]
 [  88.   NaN   67.   89.   81.]
 [  94.   79.   67.   89.   69.]
 [  88.   79.   58.   72.   63.]
 [  76.   79.   71.   67.   56.]
 [  71.   71.   NaN   56.  100.]]

I am trying to take each row, one at a time, sort it in reversed order to get max 3 values from the row and take their average. The code I tried is:

# nparr is a 2D numpy array
for entry in nparr:
    sortedentry = sorted(entry, reverse=True)
    highest_3_values = sortedentry[:3]
    avg_highest_3 = float(sum(highest_3_values)) / 3

This does not work for rows containing NaN. My question is, is there a quick way to convert all NaN values to zero in the 2D numpy array so that I have no problems with sorting and other things I am trying to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:23:51+00:00Added an answer on May 20, 2026 at 7:23 am

    This should work:

    from numpy import *
    
    a = array([[1, 2, 3], [0, 3, NaN]])
    where_are_NaNs = isnan(a)
    a[where_are_NaNs] = 0
    

    In the above case where_are_NaNs is:

    In [12]: where_are_NaNs
    Out[12]: 
    array([[False, False, False],
           [False, False,  True]], dtype=bool)
    

    A complement about efficiency. The examples below were run with numpy 1.21.2

    >>> aa = np.random.random(1_000_000)
    >>> a = np.where(aa < 0.15, np.nan, aa)
    >>> %timeit a[np.isnan(a)] = 0
    536 µs ± 8.11 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
    >>> a = np.where(aa < 0.15, np.nan, aa)
    >>> %timeit np.where(np.isnan(a), 0, a)
    2.38 ms ± 27.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    >>> a = np.where(aa < 0.15, np.nan, aa)
    >>> %timeit np.nan_to_num(a, copy=True)
    8.11 ms ± 401 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    >>> a = np.where(aa < 0.15, np.nan, aa)
    >>> %timeit np.nan_to_num(a, copy=False)
    3.8 ms ± 70.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    

    In consequence a[np.isnan(a)] = 0 is faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a 100 element numpy array. I perform some calculation on a
I have a 1D numpy array, and some offset/length values. I would like to
I have a numpy array like this a = np.array(1) Now if I want
I have a NumPy array, i want to accumulate the values of one column,
I have a 2D numpy array... there are some values in the image and
I have a problem with some numpy stuff. I need a numpy array to
How can I remove some specific elements from a numpy array? Say I have
I have an numpy array which I save to a image using savefig(). Then
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.