以下脚本尝试计算风的方向和幅度的平均值。我的月度数据框包含以下列:
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
我做的第一件事就是将DirViento列转换为弧度
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)
现在获取组件的列:u和v wind并添加到数据
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)
现在我们将数据框编入索引并分组
index=data.set_index(['Fecha','Hora'],inplace=True)
grouped = index.groupby(level=0)
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
现在获得每天的风向
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
获得的结果,我需要应用以下条件
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
如何将以前的结果添加到下一个字典
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
答案 0 :(得分:0)
您可以将自己的聚合函数应用于分组数据https://stackoverflow.com/a/10964938/2530083。所以对于你的情况你可以尝试类似的东西:
import numpy as np
def DirRes(group):
u=np.sum(group['MagViento'] * np.sin(np.deg2rad(group['DirViento'])))
v=np.sum(group['MagViento'] * np.cos(np.deg2rad(group['DirViento'])))
magres=np.sqrt(u*u+v*v)
magdir=np.rad2deg(np.arctan2(u,v))
if magdir<180:
magdir+=180
elif magdir>180:
magdir-=180
return magdir
def MagRes(group):
u=np.sum(group['MagViento'] * np.sin(np.deg2rad(group['DirViento'])))
v=np.sum(group['MagViento'] * np.cos(np.deg2rad(group['DirViento'])))
return np.sqrt(u*u + v*v)