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:
- 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]);
- You don't need semicolons in python
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
- 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.