我有一个包含8760个值的数组。我想计算每24个值的平均值,并将365个平均值作为数组返回。这是正确的还是有更简单的方法呢?
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
答案 0 :(得分:1)
每天有24个值。因此,您可以执行以下操作:
average_temps_array = [sum(map(float, hourly_temp[i:i+24])) / 24
for i in range(0, len(hourly_temp), 24)]