# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## The following tables/csv files will be created after running this notebook:
# Product
#
# This script reads the product.csv and cleans the rating as well the categories. It overwrites the originall Product.csv.
#
# The original schema contains: 'uniq_id','Product_Name','Manufacturer','Price','average_review_rating','amazon_category_and_sub_category','description'
#
#
# The new schema contains:
# 'uniq_id','Product_Name','Manufacturer','Price','average_review_rating','cat1','cat2','cat3','description'

import pandas as pd
import numpy as np

df = pd.read_csv("product.csv")

df.head()


def clean_rating(x):
    if (type(x)==str):
        score,*_ = x.split(" ")
        return float(score)
def clean_average_review_rating(df):
    df["average_review_rating"] = df["average_review_rating"].apply(lambda x:clean_rating(x))
    return df


df = clean_average_review_rating(df)


# +
def process_category_value(x,cat1,cat2,cat3):
    '''
    if x is null, all 3 categories are "All"
    if x has more than 3 categories, only first 3 categories are kept and assigned to 
        cat1, cat2, cat3.
    if x has less than 3 categories, "All" will be appended as needed
    '''
    default = ["All","All","All"]
    if (x == x):
        all_categories = x.split(" > ")
        for i in range(len(all_categories)):
            default[i] = all_categories[i]
            if (i == 2):
                break;
    
    cat1.append(default[0])        
    cat2.append(default[1])
    cat3.append(default[2])

def process_category_columns(df):
    '''
    create 3 empty lists and apply process_category_value.
    append 3 new columns to df and drop the orignal category column
    '''
    cat1 = []
    cat2 = []
    cat3 = []
    df.amazon_category_and_sub_category.apply(lambda x:process_category_value(x,cat1,cat2,cat3))
    df["Cat1"] = cat1
    df["Cat2"] = cat2
    df["Cat3"] = cat3
    df.drop(columns = "amazon_category_and_sub_category",inplace = True)
    return df
        


# -

df = process_category_columns(df)

df.head()

df.to_csv("product.csv")



StarcoderdataJupyterScriptsDedupFiltered