import numpy as np
import pandas as pd
import matplotlib.pyplot  as plt

df = pd.read_csv('../input/best-shopping/sale.csv')
df.head()
df.info()
df.dropna(inplace=True)
def delet(x):
    if 'Quantity Ordered' in x :
         return np.nan
    else:
        return x
df['Quantity Ordered'] = df['Quantity Ordered'].apply(delet)
df['Quantity Ordered'].isnull().value_counts()
df.dropna(inplace=True)
df['total'] = df['Quantity Ordered'].astype('int') * df['Price Each'].astype('float')
df.drop(['Quantity Ordered', 'Price Each'], inplace=True, axis=1)
def sep_addres(address):
    city = address.split(',')[1]
    pre_city = address.split(',')[2][:4]
    return city + pre_city
    
df['Purchase Address'] = df['Purchase Address'].apply(sep_addres)
df['Order Date'] = pd.to_datetime(df['Order Date'])
def sep_month(time):
    return time.strftime('%m')
df['month'] = df['Order Date'].apply(sep_month)
def sep_year(time):
    return time.strftime('%Y')
df['year'] = df['Order Date'].apply(sep_year)
def sep_time(time):
    return time.strftime('%H')
df['hour'] = df['Order Date'].apply(sep_time)
df.drop('Order Date', axis=1, inplace=True)
df
"""
## Visualize:
"""
import seaborn as sns
df['total'].value_counts()
df['hour'].value_counts()
g_h_t = df.pivot_table(index = 'hour', values='total', aggfunc='count')
g_h_t
g_h_t.plot(kind='bar', ylabel='count')
g_m_t = df.pivot_table(index = 'month', values='total', aggfunc='count')
g_m_t.sort_values(by='total', ascending=False)
df['month'].value_counts()
df['Purchase Address'].unique()
df['Product'].unique()
h_t_p = df.pivot_table(index = 'hour', values='total',columns='Product' ,aggfunc='count')
h_t_p
df
df['Order ID'].is_unique
df['order_product'] = df.groupby(['Order ID'])['Product'].transform(lambda x: ','.join(x))
df1 = df[['Order ID', 'order_product']].drop_duplicates()
"""
* with combination , combine 3 time then count the number of repetated time

* Counter count most repetative

"""
from itertools import combinations
from collections import Counter
count = Counter()
for row in df1['order_product']:
    #print(row)
    row_list = row.split(',')
    #print(row_list)
    count.update(Counter(combinations(row_list, 3)))
for key, value in count.most_common(10):
    print(key, value)
df['Product'].values == 'iphone'
def most_iphone(x):
        if 'iPhone' in x :
            return x
        else:
            return np.nan
df['iphone'] = df['Product'].apply(most_iphone)
df['iphone'].isnull().value_counts()
df_iphone = df.drop(columns=['order_product', 'Product'])
df_iphone = df_iphone.dropna()
df_iphone
df_iphone['Purchase Address'].value_counts()
most_iphone_sale = df_iphone.pivot_table(index='Purchase Address', columns='hour',values='total',aggfunc='count')
most_iphone_sale
most_iphone_sale.max(axis='columns')
most_iphone_sale.max(axis= 'columns').plot(kind='bar', figsize=(20,10))
df.drop(columns=['iphone', 'order_product'], inplace=True)
"""
# How Many order_id It's Not Have 1 Purchase:
# And Which Product Are More Use Fore Second Purchase 
"""
df['Order ID'].unique()
#df['Order ID'].shape
df_multi_product = df
df_multi_product['order_id'] = df['Order ID'].duplicated()
df_multi_product['order_id'] = df_multi_product['order_id'].astype('object')
def return_duplicate(x):
    if x:
        return 1
    else:
        return np.nan
df_multi_product['order_id'] = df_multi_product['order_id'].apply(return_duplicate)
df_multi_product = df_multi_product.dropna()
df_multi_product.drop('order_id',axis='columns' ,inplace=True)
most_product_sale = df_multi_product.pivot_table(index='Product', columns='Purchase Address', values='total', aggfunc='count')
"""
# You Can See Below The USB_C ,LIGHTING_CHARGING And Wired_Headphone Are Most Common Product Pepople Buy Together With Other Product
"""
most_product_sale.max(axis='columns').plot(kind='bar')
df['hour'].value_counts()
total_19 = df[ df['hour'] == '19' ] ['total']
total_18 = df[ df['hour'] == '13' ] ['total']
total_19.mean() - total_18.mean()
np.append(np.zeros(len(total_19)), np.ones(len(total_18)))
sales = np.array([
    np.append(np.zeros(len(total_19)), np.ones(len(total_18))),
    np.append(total_18, total_19)
])
sale_t = sales.T
np.random.shuffle(sale_t)
ex = np.random.randint(0, 2, len(sale_t))
ex_data = np.array([
    ex, 
    sale_t[:, 1]
])
ex_data = ex_data.T
print(ex_data[ex_data[:, 0] == 0 ][: ,1].mean())
print(ex_data[ex_data[:, 0] == 1 ][: ,1].mean())

"""
repeat 1000 time and see how many time we reach the 9.27 mean
"""
def sh_exp(n):
    ex_dif_mean = np.empty([n, 1])
    for time in np.arange(n):
        ex = np.random.randint(0, 2, len(sale_t))
        ex_data = np.array([
        ex, 
        sale_t[:, 1]]).T
        ex_dif_mean[time] = ex_data[ex_data[:, 0] == 1 ][: ,1].mean() - ex_data[ex_data[:, 0] == 0 ][: ,1].mean()
    return ex_dif_mean


        
e = sh_exp(1000)
len(e[e >= 9.271544781382346])
17 / 1000 
"""
b ehtemal %1 mobtani bar shanse bode
va 99% bar asas manadari bode


p_value = 0.1 
so reject h0 

"""
import seaborn as sns
sns.distplot(e, bins=100)