Reputation: 61
I have some data that is stored as daily means over 20 years, and I would like to create a new array that tells me the monthly means over the same period.
I am not very experienced with python, so best I could figure was something like the following:
dis2 = "array of daily means"
a2 = np.sum(dis2[:365])
b2 = np.sum(dis2[365:731])
c2 = np.sum(dis2[731:1096])
d2 = np.sum(dis2[1096:1461])
e2 = np.sum(dis2[1461:1826])
f2 = np.sum(dis2[1826:2191])
g2 = np.sum(dis2[2191:2556])
h2 = np.sum(dis2[2556: 2921])
i2 = np.sum(dis2[2921:3286])
j2 = np.sum(dis2[3286:3651])
k2 = np.sum(dis2[3651:4016])
l2 = np.sum(dis2[4016:4381])
m2 = np.sum(dis2[4381:4746])
n2 = np.sum(dis2[4746:5111])
o2 = np.sum(dis2[5111:5476])
p2 = np.sum(dis2[5476:5841])
q2 = np.sum(dis2[5841:6206])
r2 = np.sum(dis2[6206:6571])
s2 = np.sum(dis2[6571:6936])
t2 = np.sum(dis2[6936:7301])
z2 = [a2,b2,c2,d2,e2,f2,g2,h2,i2,j2, k2, l2, m2, n2, o2, p2, q2, r2, s2, t2]
z2 = [i/365 for i in z2]
The above method over course only gives me the yearly means, and to get the monthly means using this methods I'd need well over a hundred variables and I am certain there must be a simpler more efficient way of doing so, but I have not the experience to determine what it is.
In case it is at all relevant, here is how I loaded my data:
filename = 'LOWL.txt'
f2 = open(filename, 'r')
date2 = []
discharge2 = []
lines = f2.readlines()
import pandas as pd
data2 = pd.read_csv('LOWL.txt',sep='\t',header=None,usecols=[2,3])
date2 = data2[2].values
discharge2 = data2[3].values
date2 = np.array(date2, dtype = "datetime64")
dis2 = [float(i) for i in discharge2]
Upvotes: 3
Views: 42
Reputation: 2720
Combining np.mean(), slicing and a list comprehension makes for a more efficient way of doing your calculation:
z2 = [np.mean(dis2[i:(i+365)]) for i in range(0, len(dis2), 365)]
Upvotes: 1