開始使用免費開始

為天氣狀況評分

在前一個練習中,你已經計算出每天的不良天氣條件數量。這個練習中,你要用這些計數來建立一套天氣評分系統。

計數範圍是 0 到 9,並需依下列規則轉換成評分:

  • 0 轉換為 'good'
  • 14 轉換為 'bad'
  • 59 轉換為 'worse'

本練習屬於課程

使用 pandas 分析警方執法活動

檢視課程

練習說明

  • 計算 bad_conditions 欄的唯一值個數並依索引排序。(已為你完成。)
  • 建立名為 mapping 的字典,將 bad_conditions 的整數對應到指定的字串。
  • 使用 mappingbad_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(____)
編輯並執行程式碼