text_plain_output_1.png
import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_first = pd.read_csv('/kaggle/input/retail-case-study-data/prod_cat_info.csv')

df_second = pd.read_csv('/kaggle/input/retail-case-study-data/Customer.csv')

df_third = pd.read_csv('/kaggle/input/retail-case-study-data/Transactions.csv')

md = df_third.groupby(['cust_id', 'prod_cat_code', 'prod_subcat_code'])['transaction_id'].count().to_dict()

df_combined = pd.merge(df_third, df_first, left_on=['prod_cat_code', 'prod_subcat_code'], right_on=['prod_cat_code', 'prod_sub_cat_code'], how='left')

grp_df = df_combined.groupby(['cust_id', 'prod_cat', 'prod_subcat', 'tran_date'])['tran_date', 'prod_subcat'].count()

df_third.columns

df_third_part = df_third[df_third.Rate > 0].reset_index()
df_combined = pd.merge(df_third_part, df_first, left_on=['prod_cat_code', 'prod_subcat_code'], right_on=['prod_cat_code', 'prod_sub_cat_code'], how='left')

md = pd.DataFrame(df_combined[['cust_id', 'prod_cat', 'prod_subcat', 'tran_date']].value_counts())

combined_pair = df_combined[['prod_cat', 'Store_type']].drop_duplicates().to_dict('records')
contribution_dictionary = {}
for j in range(len(combined_pair)):
    df_sub_part = df_combined[(df_combined.prod_cat == combined_pair[j]['prod_cat']) & (df_combined.Store_type == combined_pair[j]['Store_type'])].reset_index()
    contribution_dictionary[combined_pair[j]['prod_cat'], combined_pair[j]['Store_type']] = np.round(100 * df_sub_part['total_amt'].sum() / df_combined['total_amt'].sum(), 2)

import operator
sorted_x = {k: v for k, v in sorted(contribution_dictionary.items(), key=lambda item: item[1], reverse=True)}

df_pie = pd.DataFrame({'item and channel': list(sorted_x.keys()), 'contribution': list(sorted_x.values())}, index=list(sorted_x.keys()))

df_pie