统计分类变量
计数可以帮助您快速了解数据概况,并发现平时不容易注意到的异常或有趣之处。本练习中,您将使用上一个练习中创建的 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)