開始使用免費開始

計算惡劣天氣條件

weather DataFrame 含有 20 個以 'WT' 開頭的欄位,每個欄位代表一種惡劣天氣條件。例如:

  • WT05 代表「雹」
  • WT11 代表「強風或具破壞性的風」
  • WT17 代表「凍雨」

在資料集的每一列中,每個 WT 欄位要嘛是 1(表示當天有出現該條件),要嘛是 NaN(表示當天沒有出現)。

在這個練習中,你要透過計算每一列中 1 的個數,來量化每天天氣「有多惡劣」。

本練習屬於課程

使用 pandas 分析警方執法活動

檢視課程

練習說明

  • weather 中從 WT01WT22 的欄位複製到一個名為 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
編輯並執行程式碼