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 8814221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:03:38+00:00 2026-06-14T04:03:38+00:00

Possible Duplicate: avarage of a number of arrays with numpy without considering zero values

  • 0

Possible Duplicate:
avarage of a number of arrays with numpy without considering zero values

I am working on numpy and I have a number of arrays with the same size and shape. They are 500*500. It has some Null values. I want to have an array that is result of one by one element average of my original arrays. For example:

A=[ 1 Null 8 Null; Null 4 6 1]
B=[ 8 5 8 Null; 5 9 5 3]

the resulting array should be like:

C=[ 4.5 5 8 Null; 5 6.5 5.5 2]

How can I do that?

  • 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-06-14T04:03:39+00:00Added an answer on June 14, 2026 at 4:03 am

    Update: As of NumPy 1.8, you could use np.nanmean instead of scipy.stats.nanmean.


    If you have scipy, you could use scipy.stats.nanmean:

    In [2]: import numpy as np
    
    In [45]: import scipy.stats as stats
    
    In [3]: nan = np.nan
    
    In [43]: A = np.array([1, nan, 8, nan, nan, 4, 6, 1])   
    In [44]: B = np.array([8, 5, 8, nan, 5, 9, 5, 3])  
    In [46]: C = np.array([A, B])    
    In [47]: C
    Out[47]: 
    array([[  1.,  nan,   8.,  nan,  nan,   4.,   6.,   1.],
           [  8.,   5.,   8.,  nan,   5.,   9.,   5.,   3.]])
    
    In [48]: stats.nanmean(C)
    Warning: invalid value encountered in divide
    Out[48]: array([ 4.5,  5. ,  8. ,  nan,  5. ,  6.5,  5.5,  2. ])
    

    You can find other numpy-only (masked-array) solutions, here. Namely,

    In [60]: C = np.array([A, B])    
    In [61]: C = np.ma.masked_array(C, np.isnan(C))    
    In [62]: C
    Out[62]: 
    masked_array(data =
     [[1.0 -- 8.0 -- -- 4.0 6.0 1.0]
     [8.0 5.0 8.0 -- 5.0 9.0 5.0 3.0]],
                 mask =
     [[False  True False  True  True False False False]
     [False False False  True False False False False]],
           fill_value = 1e+20)
    
    In [63]: np.mean(C, axis = 0)
    Out[63]: 
    masked_array(data = [4.5 5.0 8.0 -- 5.0 6.5 5.5 2.0],
                 mask = [False False False  True False False False False],
           fill_value = 1e+20)
    
    In [66]: np.ma.filled(np.mean(C, axis = 0), nan)
    Out[67]: array([ 4.5,  5. ,  8. ,  nan,  5. ,  6.5,  5.5,  2. ])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Find the min number in all contiguous subarrays of size l of
Possible Duplicate: Oracle SQL - How to Retrieve highest 5 values of a column
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Undefined reference to vtable I have a class Student on the file
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file

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.