The following script try to calculate the resulting average of the direction and magnitude of the wind well as daily averages of Temperature, Moisture and Sum of Precipitation. My monthly dataframe has the following column:
data
Fecha Hora DirViento MagViento Temperatura Humedad PreciAcu
0 2011/07/01 00:00 318 6.6 21.22 100 1.7
1 2011/07/01 00:15 342 5.5 21.20 100 1.7
2 2011/07/01 00:30 329 6.6 21.15 100 4.8
3 2011/07/01 00:45 279 7.5 21.11 100 4.2
4 2011/07/01 01:00 318 6.0 21.16 100 2.5
The first thing I do is convert to radians the DirViento column
dir_rad=[]
for i in range(0, len(data['DirViento'])):
dir_rad.append(data['DirViento'][i]*(pi/180.0))
data['DirViento']=around(dir_rad,1)
Now get the columns of the components: u and v wind and add to data
Uviento=[]
Vviento=[]
for i in range(0,len(data['MagViento'])):
Uviento.append(data['MagViento'][i]*sin(data[DirViento][i]))
Vviento.append(data['MagViento'][i]*cos(data[DirViento][i]))
data['u']=around(Uviento,1)
data['v']=around(Vviento,1)
data
Data columns:
Fecha 51 non-null values
Hora 51 non-null values
DirViento 51 non-null values
MagViento 51 non-null values
Temperatura 51 non-null values
Humedad 51 non-null values
PreciAcu 51 non-null values
u 51 non-null values
v 51 non-null values
dtypes: float64(6), int64(2), object(2)
Now using "pandas" and we indexed the dataframe and grouped
index=data.set_index(['Fecha','Hora'],inplace=True)
grouped = index.groupby(level=0)
for example
data['u']
Fecha Hora
2011/07/01 00:00 -4.4
00:15 -1.7
00:30 -3.4
00:45 -7.4
01:00 -4.0
2011/07/02 00:00 -4.5
00:15 -4.2
00:30 -7.6
00:45 -3.8
01:00 -2.0
2011/07/03 00:00 -6.3
00:15 -13.7
00:30 -0.3
00:45 -2.5
01:00 -2.7
Now get resultant wind direction for each day
grouped.apply(lambda x: ((scipy.arctan2(mean(x['uu']),mean(x['vv'])))/(pi/180.0)))
Fecha
2011/07/01 -55.495677
2011/07/02 -39.176537
2011/07/03 -51.416339
The result obtained, I need to apply the following conditions
for i in grouped.apply(lambda x: ((scipy.arctan2(mean(x['uu']),mean(x['vv'])))/(pi/180.0))):
if i < 180:
i=i+180
else:
if i > 180:
i=i-180
else:
i=i
print i
124.504323033
140.823463279
128.5836605
Why not display the column for dates? how this is achieved?
How to include the above result to a column that is called DirRes? Once the DirRes column was created, how add it to the following dictionary:
stat_cea = grouped.agg({'MagRes':np.mean,'DirRes':np.mean,'Temperatura':np.mean,'Humedad':np.mean,'PreciAcu':np.sum})
stat_cea
Fecha DirRes Humedad PreciAcu Temperatura
2011/07/01 100.000000 30.4 21.367059
2011/07/02 99.823529 18.0 21.841765
2011/07/03 99.823529 4.0 21.347059
Your help is vital