Classify The Wind Direction In Several Classes
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 wi
Solution 1:
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)]
printdf
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
Solution 2:
You can use loc
:
import pandas as pd
df = pd.DataFrame({"wd": pd.Series([20.1,50,8.4 ])})
print df
wd
020.1150.028.4print (df.wd >= 11.25 ) & (df.wd < 33.75 )
0True1False2False
Name: wd, dtype: bool
df.loc[(df.wd >= 11.25 ) & (df.wd < 33.75 ), 'new'] = 'NNE'
df.loc[(df.wd >= 33.75 ) & (df.wd < 56.25 ), 'new'] = 'NE'print df
wd new020.1 NNE
150.0 NE
28.4 NaN
Post a Comment for "Classify The Wind Direction In Several Classes"