Domanda

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
È stato utile?

Soluzione

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)]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top