為天氣狀況評分
在前一個練習中,你已經計算出每天的不良天氣條件數量。這個練習中,你要用這些計數來建立一套天氣評分系統。
計數範圍是 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(____)