<jupyter_start><jupyter_text>1. Bài 1:● Tìm 10 departments bán được nhiều sản phẩm nhất● Vẽ biểu đồ cột và tinh chỉnh hiển thị số liệu sao cho thuận lợi trình bày, ngườixem chỉ cần nhìn biểu đồ là nắm được đầy đủ thông tin(cần có title, số liệu cho từng cột, tên các cột)2. Bài 2:● Ngoài 10 aisle/department bán được nhiều sản phẩm nhất, tính tổng số sảnphẩm đã bán được của phần còn lại● Vẽ biểu đồ tree-map hiển thị kết quả trên<jupyter_code>import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
np.set_printoptions(precision=3, suppress=True)
# pd.set_option('display.float_format', lambda x: '%.3f' % x)

print(pd.__version__)<jupyter_output>1.3.4<jupyter_text>Read csv file<jupyter_code>filepath = 'products.csv'
product_info = pd.read_csv(filepath)
product_info.head()
N = 5e5
filepath = 'order_products__prior.csv'
order_product_info = pd.read_csv(filepath, chunksize=N)
order_product_info_chunk = next(order_product_info)
order_product_info_chunk.head()
filepath = 'departments.csv'
department_info = pd.read_csv(filepath)
department_info.head()<jupyter_output><empty_output><jupyter_text>Order_products_prior has product_id (the order products' info) Products has product_id, department_id (the products' info)Departments has department_id, department(the department' info)<jupyter_code>def selectDeptId(order_prod_id):
    # print(order_prod_id.head())
    product_dept_id = product_info[['product_id', 'department_id']]
    order_dept_id = pd.merge(order_prod_id, product_dept_id, on='product_id')['department_id']
    dept_id_count = order_dept_id.value_counts().reset_index()
    dept_id_count.columns = ['department_id', 'counts']
    return dept_id_count

chunkNum = 5e5
file = 'order_products__prior.csv'
order_products = pd.read_csv(file, chunksize=chunkNum, usecols=['product_id'])
chunk = next(order_products)
total_dept_count = selectDeptId(chunk)

for i, chunk in enumerate(order_products):
    chunk_count = selectDeptId(chunk)
    total_dept_count = pd.concat([total_dept_count, chunk_count])
    # print(total_dept_count.shape)
    
print(total_dept_count.head())<jupyter_output>department_id  counts
0              4  145463
1             16   83779
2             19   44513
3              7   41613
4              1   34773<jupyter_text>10 departments have the largest products ordered<jupyter_code>total_dept_count = total_dept_count.groupby('department_id').sum().reset_index()
total_dept_count.head()

# 10 departments with largest ordered products 
n_dept_id = total_dept_count.nlargest(10, columns='counts')
n_dept_id<jupyter_output><empty_output><jupyter_text>Bar Plot<jupyter_code>n_dept_id_name = pd.merge(n_dept_id, department_info, on='department_id')
n_dept_id_name
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot()
ax = n_dept_id_name.plot.bar(ax=ax, x='department', y='counts', rot=60, width=0.8, 
                           color='red', legend=None)
# ax.bar_label(container=ax.containers[0], padding=2, fontsize=6, rotation=0)
ax.set_xticks(n_dept_id_name.index)
ax.set_xticklabels(n_dept_id_name['department'])
plt.tight_layout()
plt.show()
plt.close('all')
# The rest of the ordered products
total_dept_count.head()
n_dept_id.head()
# rest_order_products 
# a = total_dept_count[~total_dept_count.apply(tuple,1).isin(n_dept_id.apply(tuple,1))]
rest_prod_id = total_dept_count[~total_dept_count['counts'].isin(n_dept_id['counts'])]
rest_order_prod_name = pd.merge(rest_prod_id, department_info, on='department_id')
rest_order_prod_name<jupyter_output><empty_output><jupyter_text>Treemap plot<jupyter_code>import squarify

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
labels = rest_order_prod_name['department']
size = rest_order_prod_name['counts']
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']

ax = squarify.plot(sizes = size, label=labels, pad=False, color=colors, alpha=.85)
plt.axis('off')
plt.show()
plt.close('all')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11885143570536828, 'fraction_numerical': 0.02746566791510612, 'mean_word_length': 6.296903460837887, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '32467', 'license': 'MIT', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1632, 'n_words': 292, 'path': 'hw8/hw8.ipynb', 'repo_name': 'RachelKes/Data-Analytics', 'size': '4005'}