一、需求分析
如题
二、程序设计
这些方位从北开始,顺时针排列,每个方位之间的角度为22.5度。这种细分有助于更精确地描述风的方向,特别是在气象观测和预报中。
三、完整代码
import pandas as pd
# First, let's read the newly uploaded Excel file to see its structure and content.
file_path_new = '/mnt/data/test.xlsx'
data_new = pd.read_excel(file_path_new)
# Display the first few rows of the dataframe to understand its structure
data_new.head()
# The data structure is the same as the previously uploaded file. Now we will convert the angles
# in the '2分钟平均风向' column to the corresponding 16 compass directions.
# Define a function to convert wind direction angle to one of the 16 compass directions
def angle_to_direction(angle):
if 348.75 <= angle or angle < 11.25:
return 'N'
elif 11.25 <= angle < 33.75:
return 'NNE'
elif 33.75 <= angle < 56.25:
return 'NE'
elif 56.25 <= angle < 78.75:
return 'ENE'
elif 78.75 <= angle < 101.25:
return 'E'
elif 101.25 <= angle < 123.75:
return 'ESE'
elif 123.75 <= angle < 146.25:
return 'SE'
elif 146.25 <= angle < 168.75:
return 'SSE'
elif 168.75 <= angle < 191.25:
return 'S'
elif 191.25 <= angle < 213.75:
return 'SSW'
elif 213.75 <= angle < 236.25:
return 'SW'
elif 236.25 <= angle < 258.75:
return 'WSW'
elif 258.75 <= angle < 281.25:
return 'W'
elif 281.25 <= angle < 303.75:
return 'WNW'
elif 303.75 <= angle < 326.25:
return 'NW'
elif 326.25 <= angle < 348.75:
return 'NNW'
# Apply the function to the '2分钟平均风向' column to create a new 'Direction' column
data_new['Direction'] = data_new['2分钟平均风向'].apply(angle_to_direction)
# Display the dataframe with the new 'Direction' column
data_new.head()
四、结果
转换完成,现在新的数据帧 data_new
包含了一个名为 ‘Direction’ 的新列,它将 ‘2分钟平均风向’ 的角度值映射到了16个方位之一。以下是转换后的前几行数据: