Đếm biến phân loại
Đếm là cách tuyệt vời để có cái nhìn tổng quan về dữ liệu và phát hiện những điểm thú vị mà bình thường bạn có thể bỏ qua. Trong bài tập này, bạn sẽ đếm số lượng của từng loại cửa hàng và số lượng của từng phòng ban bằng cách dùng các DataFrame bạn đã tạo ở bài trước:
# Loại bỏ các kết hợp store/type trùng lặp
store_types = sales.drop_duplicates(subset=["store", "type"])
# Loại bỏ các kết hợp store/department trùng lặp
store_depts = sales.drop_duplicates(subset=["store", "department"])
Các DataFrame store_types và store_depts bạn đã tạo ở bài trước đã có sẵn, và pandas đã được nhập với bí danh pd.
Bài tập này là một phần của khóa học
Xử lý dữ liệu với pandas
Hướng dẫn bài tập
- Đếm số cửa hàng của mỗi
typetrongstore_types. - Đếm tỷ lệ cửa hàng của mỗi
typetrongstore_types. - Đếm số cửa hàng của mỗi
departmenttrongstore_depts, sắp xếp số đếm giảm dần. - Đếm tỷ lệ cửa hàng của mỗi
departmenttrongstore_depts, sắp xếp tỷ lệ giảm dần.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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)