2013-10-20 1 views
5

다음 스크립트는 바람의 방향과 크기의 결과 평균을 계산합니다. 월별 dataframe는 다음과 같은 열이 있습니다 U와 V의 바람과 데이터에 추가 :groupby로 그룹화 된 계층 적 데이터 프레임에 새 열을 추가하는 방법

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) 

구성 요소의 열을 얻을 수를 라디안으로 변환하면된다 우선

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) 

이제 우리는 dataframe 색인 및

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 

, 나는 다음과 같은 조건 다음 사전

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 
에 이전 결과를 추가하는 방법
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 

을 적용 할 필요가 매일 결과 바람의 방향을 얻을

답변

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) 
관련 문제