計算類別變數
計數能幫助你快速掌握資料全貌,也能發現原本可能忽略的有趣現象。在本練習中,你會使用前一題建立的 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_types 與 store_depts 這兩個 DataFrame 已可使用,且已將 pandas 以 pd 匯入。
本練習屬於課程
使用 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)