計算惡劣天氣條件
weather DataFrame 含有 20 個以 'WT' 開頭的欄位,每個欄位代表一種惡劣天氣條件。例如:
WT05代表「雹」WT11代表「強風或具破壞性的風」WT17代表「凍雨」
在資料集的每一列中,每個 WT 欄位要嘛是 1(表示當天有出現該條件),要嘛是 NaN(表示當天沒有出現)。
在這個練習中,你要透過計算每一列中 1 的個數,來量化每天天氣「有多惡劣」。
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 將
weather中從WT01到WT22的欄位複製到一個名為WT的新 DataFrame。 - 計算
WT每一列的總和,並將結果存到weather中名為bad_conditions的新欄位。 - 將
bad_conditions中的遺漏值替換為0。(此步驟已為你完成。) - 建立
bad_conditions的直方圖並顯示圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Copy 'WT01' through 'WT22' to a new DataFrame
WT = weather.____[____]
# Calculate the sum of each row in 'WT'
weather['bad_conditions'] = WT.____(____)
# Replace missing values in 'bad_conditions' with '0'
weather['bad_conditions'] = weather.bad_conditions.fillna(0).astype('int')
# Create a histogram to visualize 'bad_conditions'
# Display the plot