開始使用免費開始

計算類別變數

計數能幫助你快速掌握資料全貌,也能發現原本可能忽略的有趣現象。在本練習中,你會使用前一題建立的 DataFrame,統計各類型商店的數量,以及各部門代號的數量:

# Drop duplicate store/type combinations
store_types = sales.drop_duplicates(subset=["store", "type"])

# Drop duplicate store/department combinations
store_depts = sales.drop_duplicates(subset=["store", "department"])

你在上一題建立的 store_typesstore_depts 這兩個 DataFrame 已可使用,且已將 pandaspd 匯入。

本練習屬於課程

使用 pandas 進行資料操作

檢視課程

練習說明

  • store_types 中,計算每種商店 type 的家數。
  • store_types 中,計算每種商店 type 的家數比例。
  • store_depts 中,計算每個 department 的商店家數,並將計數以遞減排序。
  • store_depts 中,計算每個 department 的商店家數比例,並將比例以遞減排序。

動手互動練習

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

# Count the number of stores of each type
store_counts = ____
print(store_counts)

# Get the proportion of stores of each type
store_props = ____
print(store_props)

# Count the number of stores for each department and sort
dept_counts_sorted = ____
print(dept_counts_sorted)

# Get the proportion of stores in each department and sort
dept_props_sorted = ____.____(sort=____, normalize=____)
print(dept_props_sorted)
編輯並執行程式碼