I have a Pandas dataframe like below with cardinal wind directions :
| X |
+----------+
|N |
|NE |
|NNE |
|SSE |
|WSW |
+----------+
Question is how can I convert the wind directions to degrees and store in a dataframe/excel ?
Firstly create a dictionary:
d={'N':0, 'NNE':22.5,"NE":45,"ENE":67.5, 'E':90,'ESE':112.5, 'SE':135,'SSE':157.5, 'S':180,'SSW':202.5, 'SW':225,'WSW':247.5, 'W':270,'WNW':292.5,'NW':315,'NNW':337.5, 'N':0,'North':0,'East':90,'West':270,'South':180}
Finally use strip()
method and map()
method:
df['X']=df['X'].str.strip().map(d)
OR
use strip()
and replace()
method
df['X']=df['X'].str.strip().replace(d)
Output of df
:
X
0 0.0
1 45.0
2 22.5
3 157.5
4 247.5