-1

I have an array with 8760 values. I want to calculate the average for every 24 values and return the 365 average values as an array. Is this correct or is there a more simple way of doing this?

temps = data['temperature'] #Fetching temperaturedata from dictionary temps_array = np.asarray(temps) #Converting temps list to array averages = daily_mean_temp(temps_array) #Running function that calculates mean def daily_mean_temp(hourly_temp): average_temps = [x.mean() for x in np.array_split( [float(x) for x in hourly_temp], 365)] #This is a list average_temps_array = np.asarray(average_temps) #Converting from list to array return average_temps_array #return array with mean values 
6
  • What's wrong with sum(mylist)/len(mylist) for computing the average of a list? Commented Mar 14, 2014 at 15:49
  • @Emmet: I think this is a bit more complex. Commented Mar 14, 2014 at 15:50
  • Good answers on this here: stackoverflow.com/questions/13728392/… Commented Mar 14, 2014 at 15:50
  • You should update your previous question instead of asking it again. Commented Mar 14, 2014 at 15:55
  • "Is this correct" - does it give the answer you were expecting? Commented Mar 14, 2014 at 16:05

1 Answer 1

1

There are 24 values for each day. Therefore, you can do something like:

average_temps_array = [sum(map(float, hourly_temp[i:i+24])) / 24 for i in range(0, len(hourly_temp), 24)] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.