开始使用免费开始使用

统计分类变量

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