0

new to coding here, does anyone know how to remove nan values from a loop that is creating a monthly average? This is my code:

c = 0
av = np.zeros([12,1])

for i in range(1,13):
    av[c] = np.nanmean(dataz[time.month==i])
    c = c+1

print(av)

this is the outcome:

[[82.83672087]
 [53.67317073]
 [48.88129252]
 [52.04382826]
 [        nan]
 [        nan]
 [        nan]
 [        nan]
 [        nan]
 [        nan]
 [52.25087788]
 [81.22294607]]

Any help greatly appreciated. Please try to be as simple as possible as this is my first time doing Python and I've already tried looking for fixes for this but its too complicated to understand! :)

Alex Metsai
  • 1,837
  • 5
  • 12
  • 24
Alex P
  • 1
  • 1
    welcome to stackoverflow! check out https://stackoverflow.com/questions/944700/how-can-i-check-for-nan-values and maybe read about list comprehensions (av doesn't need to be a numpy array). also, what exactly are you trying to do? remove the nan values, or replace them with 0, or something entirely different? – jemand771 May 31 '21 at 11:21
  • Thank you! ah okay, well i am basically copying off a tutorial my professor gave me so i dont actually know where i am going wrong! i would just like to replace them with 0 – Alex P May 31 '21 at 12:41

2 Answers2

0

Since you are asking for a "as simple as possible" method, you can append all non-nan values to a new list.

av_new = []
for item in av:
    if not np.isnan(item):
        av_new.append(item)
Alex Metsai
  • 1,837
  • 5
  • 12
  • 24
0

Although most of these things aren't specifically about your question (and this isn't codereview :b) I want to give you some general hints:

  1. you don't really need the variable c since it's always equal to i - 1. So the line using c turns into av[i - 1] = np.nanmean(dataz[time.month==i]). However, this is kind of hard to read, so I'd just do:
for i in range(12):
    av[i] = np.nanmean(dataz[time.month==i+1]);
  1. You don't need semicolons in python
  2. av doesn't need to be a numpy array since you're not doing a lot of math with it. For learning the basics of python, try to stick with the builtin types like list and work your way up to math libraries like numpy, pandas, ... later. Maybe check out w3schools' guide on lists
  3. Small note: av is currently an array of arrays, with each of the inner arrays having exactly one element. In case this isn't a requirement, you should try to simplify that (And search where the extra array is coming from). Leave a comment if this doesn't work and I'll try to edit my answer.

As per your comment, you want to replace nan values with zero. I'd do it this way:

av = []
for i in range(12):
    month_average = np.nanmean(dataz[time.month==i+1])
    if month_average != month_average:
        month_average = 0
    av.append(month_average)

If the month_average != month_average check for NaN is too unholy for you (see comments under this answer) you can also use something like np.isnan(month_average )

Finally, if you want to impress people:

av = [x for x in [np.nanmean(dataz[time.month==i+1]) for i in range(12)] if x == x else 0]

Check out list comprehensions if you want to know how this works.

jemand771
  • 457
  • 1
  • 6
  • 17