I have a 9X51100 two-dimensional array (9 different datasets, 51,100 daily files) and I'm attempting to take 140 yearly averages of the data and append them to a new array. However, numpy.append needs two arguments and I end up appending more than I want to, I believe. Here's the code and the error I get.
import numpy as np
citydata = ['bcm2.a2.USC00101022.tmax.1960.2099.txt','bcm2.a2.USC00362682.tmax.1960.2099.txt','bcm2.a2.USC00415411.tmax.1960.2099.txt',
'ccsm.a2.USC00101022.tmax.1960.2099.txt','ccsm.a2.USC00362682.tmax.1960.2099.txt','ccsm.a2.USC00415411.tmax.1960.2099.txt',
'pcm.a2.USC00101022.tmax.1960.2099.txt','pcm.a2.USC00362682.tmax.1960.2099.txt','pcm.a2.USC00415411.tmax.1960.2099.txt']
year = np.asanyarray([(np.genfromtxt(item, skip_header=1)[:,0]) for item in citydata])
tmax = np.asanyarray([(np.genfromtxt(item, skip_header=1)[:,3]*(9./5.))+32 for item in citydata])
tmax_avg = np.zeros([9,140]) #initialize averaged array
for i in range(0,8,1):
for yr in years:
toavg = (year == yr)
tmax_avg[i,:] = np.append(tmax_avg,np.average(tmax[toavg]))
ValueError Traceback (most recent call last)
<ipython-input-23-a8a57d128124> in <module>()
8 for yr in years:
9 toavg = (year == yr)
10--> tmax_avg[i,:] = np.append(tmax_avg,np.average(tmax[toavg]))
ValueError: could not broadcast input array from shape (1261) into shape (140)
It also just seems to be only wanting to give me one 140-value array instead of a 9X140 array. Any help with either the append or looping problems? Thanks.
pandasproblem into anumpyframework. Now with enough work, anything you can do withpandasyou can do withnumpy-- or with plain lists, for that matter -- but you're basically reimplementingdf.groupby("year").mean(). [Could also useresample.]pandassometime in the near future, but there never seem to be enough hours in the day. I know most of what's going on innumpyso that's why I continue to try and do most of my work with it.yearandtmax, but wouldtmax_avg[i][toavg] = np.average(tmax[toavg])work. Or maybetmax_avg[i,j] = np.average(tmax[toavg])with the for-loop replaced byfor j, yr in enumerate(years):work?yearandtmaxor upload some short example files and link to them. As @Wicket says it's not clear how your data is shaped.