開始使用免費開始

在樞紐分析表上進行計算

樞紐分析表會填入彙總統計,不過這只是找出洞見的第一步。你常常還需要在此基礎上做進一步計算。常見的作法是找出最大值或最小值出現在哪些列或欄。

回想第 1 章的內容,你可以在方括號中使用邏輯條件,輕鬆地對 Series 或 DataFrame 做子集篩選,找出感興趣的列。例如:series[series > value]

已將 pandas 載入為 pd,且 DataFrame temp_by_country_city_vs_year 可用。 此 DataFrame 的 .head() 如下所示,僅顯示部分年份欄位:

country city 2000 2001 2002 2013
Afghanistan Kabul 15.823 15.848 15.715 16.206
Angola Luanda 24.410 24.427 24.791 24.554
Australia Melbourne 14.320 14.180 14.076 14.742
Sydney 17.567 17.854 17.734 18.090
Bangladesh span translate="no">Dhaka 25.905 25.931 26.095 26.587

本練習屬於課程

使用 pandas 進行資料操作

檢視課程

練習說明

  • 計算每一年的平均氣溫,指派給 mean_temp_by_year
  • 篩選 mean_temp_by_year,找出平均氣溫最高的那一年。
  • 計算每個城市(跨欄) 的平均氣溫,指派給 mean_temp_by_city
  • 篩選 mean_temp_by_city,找出平均氣溫最低的那個城市。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Get the worldwide mean temp by year
mean_temp_by_year = temp_by_country_city_vs_year.____

# Filter for the year that had the highest mean temp
print(mean_temp_by_year[____])

# Get the mean temp by city
mean_temp_by_city = temp_by_country_city_vs_year.____

# Filter for the city that had the lowest mean temp
print(mean_temp_by_city[____])
編輯並執行程式碼