将风向分为几类 [英] Classify the wind direction in several classes

查看:135
本文介绍了将风向分为几类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.
我有一个数据框 df ,其中包含名为 date wd 的两列.
wd 表示风向,范围为(0〜360).
因此, df 表示特定时间范围内某处的风向.

Here is my question.
I have one dataframe df which contain two columns named date and wd.
And the wd means the wind direction which range from (0~360).
So, the df represent the wind direction of somewhere in certain time frame.

我想将这些风向分为16类:
http://7xrn7f.com1.z0.glb. clouddn.com/16-3-8/30080798.jpg

此处显示范围.

http://7xrn7f.com1.z0 .glb.clouddn.com/16-3-8/8398960.jpg

这是我现在可以处理的:

This is what I can deal with now:

wd_stat = []
for i in range(0,len(df),1):
    wd = df.wd.iloc[i]
    ### NNE 11.25-33.75
    if 11.25 <= wd < 33.75:
       wd_stat.append("NNE")    
    ### NE 33.75-56.25   
    if (33.75 <=wd < 56.25):
       wd_stat.append("NE")
    ### ENE 56.25 - 78.75    
    if (56.25 <=wd < 78.75):
       wd_stat.append("ENE") 
    if (78.75 <=wd < 101.25):
       wd_stat.append("E") 
    if (101.25 <=wd < 123.75):
        wd_stat.append("ESE") 
      .....not done yet......

我的方法不灵活而且不可行.
任何人都可以提出一些建议来高效处理此类分类问题(数字范围转换为某些字符).

My method was inflexible and dump.
Can anyone give some advices to deal the classify problem like this(number range into certain characters) in high efficience.

推荐答案

执行此类操作的一种好方法是使用

A nice way to do these kind of things is by using numpy.digitize(). It takes an array of bins and values and returns the index into which bin each value falls. Use these indices in a matching string array to get what you want:

import numpy as np
import pandas as pd

df = pd.DataFrame({"wd": pd.Series([20.1,50,8.4,359,243,123])})

directions = np.array('N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW N'.split())
bins = np.arange(11.25, 372, 22.5)
df['wd_stat'] = directions[np.digitize(df['wd'], bins)]
print df

      wd wd_stat
0   20.1     NNE
1   50.0      NE
2    8.4       N
3  359.0       N
4  243.0     WSW
5  123.0     ESE

这篇关于将风向分为几类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆