为天气状况打分
在上一个练习中,您统计了每天的不良天气状况数量。本练习将使用这些计数为天气创建一个评分体系。
计数范围为 0 到 9,需要按如下规则转换为评分:
- 将
0转换为'good' - 将
1到4转换为'bad' - 将
5到9转换为'worse'
本练习是课程的一部分
使用 pandas 分析警务活动
练习说明
- 统计
bad_conditions列中的唯一值,并按索引排序。(已为您完成。) - 创建名为
mapping的字典,将bad_conditions的整数映射到指定的字符串。 - 使用
mapping将bad_conditions的整数转换为字符串,并将结果存入名为rating的新列中。 - 统计
rating中的唯一值,以验证整数是否已正确转换为字符串。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Count the unique values in 'bad_conditions' and sort the index
print(weather.bad_conditions.value_counts().sort_index())
# Create a dictionary that maps integers to strings
mapping = {0:'good', 1:'bad', 2:'bad', ____}
# Convert the 'bad_conditions' integers to strings using the 'mapping'
weather['rating'] = weather.bad_conditions.____
# Count the unique values in 'rating'
print(____)