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

# ### Note
# * Instructions have been included for each segment. You do not have to follow them exactly, but they are included to help you think through the steps.

# Dependencies and Setup
import pandas as pd
import numpy as np

# +
# File to Load (Remember to Change These)
file_csv = '../HeroesOfPymoli/purchase_data.csv'

# Read Purchasing File and store into Pandas data frame
purchase_df = pd.read_csv(file_csv)
purchase_df.head()
# -

# ## Player Count

# * Display the total number of players
#

Players = purchase_df['SN'].drop_duplicates().value_counts().count()
Players

Total_players= pd.DataFrame({"Total Players": Players}, index=[0])
Total_players
#100%

# ## Purchasing Analysis (Total)

# * Run basic calculations to obtain number of unique items, average price, etc.
#
#
# * Create a summary data frame to hold the results
#
#
# * Optional: give the displayed data cleaner formatting
#
#
# * Display the summary data frame
#

Values = purchase_df['Purchase ID'].count()
Values

players = purchase_df['Item ID'].nunique()
players

Total = purchase_df['Price'].sum()
Total

Mean = purchase_df['Price'].mean()
Mean

Summary = pd.DataFrame({'Number of Unique Items': players,
                        'Average Price': '${:.2f}'.format(Mean), 
                                'Number of Purchases': Values, 
                        'Total Revenue': '${:.2f}'.format(Total)
                       },index=[0])
Summary
#purchase_df.colums=['Number of Unique Items','Average Price', 'Number of Purchases', 'Total Revenue']
#purchase_df
#100%

# ## Gender Demographics

# * Percentage and Count of Male Players
#
#
# * Percentage and Count of Female Players
#
#
# * Percentage and Count of Other / Non-Disclosed
#
#
#

gender = purchase_df.drop_duplicates('SN')
gender

count =gender['Gender'].value_counts()
count

# +
Percentage= (gender['Gender'].value_counts()/Players*100)

Percentage
# -

Demo_df = pd.DataFrame({'Total Count': count,
                           'Percentage of Players': Percentage.map('{:.2f}%'.format)
                           })
Demo_df
#100%

#
# ## Purchasing Analysis (Gender)

# * Run basic calculations to obtain purchase count, avg. purchase price, avg. purchase total per person etc. by gender
#
#
#
#
# * Create a summary data frame to hold the results
#
#
# * Optional: give the displayed data cleaner formatting
#
#
# * Display the summary data frame

P_A = purchase_df.groupby('Gender')
PA_count = P_A['Gender'].count()
PA_count

Avg  = P_A['Price'].mean()
Avg

Total_num = P_A['Price'].sum()
Total_num

P_A_G = pd.DataFrame({'Purchase Count': PA_count,
                      'Avg Purchase Price': Avg.map('${:,.2f}'.format),
                      'Total Revenue': Total_num.map('${:,.2f}'.format)})
P_A_G
#100%

# ## Age Demographics

# * Establish bins for ages
#
#
# * Categorize the existing players using the age bins. Hint: use pd.cut()
#
#
# * Calculate the numbers and percentages by age group
#
#
# * Create a summary data frame to hold the results
#
#
# * Optional: round the percentage column to two decimal points
#
#
# * Display Age Demographics Table
#

age= purchase_df.groupby('Age')
age.describe()

# +
bins =[0, 9, 14, 19, 24, 29, 34, 39, 100]

labels =['<10', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40+']

# +
purchase_df['age_group']= pd.cut(purchase_df['Age'], bins, labels=labels)

age_p = purchase_df.groupby('age_group').SN.nunique()
age_p
age_sum = purchase_df.groupby('age_group').SN.nunique().sum()
age_sum
Age_D = pd.DataFrame({'Total Count': age_p,
                    'Percentage of Players': age_p/ age_sum * 100})
Age_D
#100%
# -

# ## Purchasing Analysis (Age)

# * Bin the purchase_data data frame by age
#
#
# * Run basic calculations to obtain purchase count, avg. purchase price, avg. purchase total per person etc. in the table below
#
#
# * Create a summary data frame to hold the results
#
#
# * Optional: give the displayed data cleaner formatting
#
#
# * Display the summary data frame

# +
bins =[0, 9, 14, 19, 24, 29, 34, 39, 100]

labels =['<10', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40+']
# -

P_age= purchase_df[['age_group','Purchase ID']]
P_age
P_val= purchase_df[['age_group','Price']]
P_val

P = P_age.groupby('age_group').count()
P
P_v1 = purchase_df.groupby(['age_group']).count()['Price']
P_v1

P_V = P_val.groupby('age_group').sum()
P_V
P_V2 = purchase_df.groupby(['age_group']).sum()['Price']
P_V2

M_P = P_val.groupby('age_group').mean()
M_P
M_P2 = purchase_df.groupby(['age_group']).mean()['Price']
M_P2

Purchase_c = pd.DataFrame({'Purchase Count': P_v1,
                           'Average Purchase Price': M_P2.map('${:,.2f}'.format),
                           'Total Purchase Value': P_V2.map('${:,.2f}'.format),
                          'Avg Total Purchase per Person':(0)})
Purchase_c 
#help
#85%

# ## Top Spenders

# * Run basic calculations to obtain the results in the table below
#
#
# * Create a summary data frame to hold the results
#
#
# * Sort the total purchase value column in descending order
#
#
# * Optional: give the displayed data cleaner formatting
#
#
# * Display a preview of the summary data frame
#
#

Spend= purchase_df['SN'].value_counts().sort_values(ascending = False)
Spend

Avg_S = purchase_df['Price'].groupby(purchase_df['SN']).mean().sort_values(ascending = False)
Avg_S
#help

T_P = purchase_df['Price'].groupby(purchase_df['SN']).sum().sort_values(ascending= False)
T_P

T_S = pd.DataFrame({'Purchase Count': Spend,
                   'Avg Purchase Price': Avg_S.map('${:,.2f}'.format),
                   'Total Purchase Value': T_P})
T_S.sort_index().round(2)

T_Sv = T_S.sort_values(by='Total Purchase Value', ascending=False)
T_Sv.head(6)
#100%

# ## Most Popular Items

# * Retrieve the Item ID, Item Name, and Item Price columns
#
#
# * Group by Item ID and Item Name. Perform calculations to obtain purchase count, item price, and total purchase value
#
#
# * Create a summary data frame to hold the results
#
#
# * Sort the purchase count column in descending order
#
#
# * Optional: give the displayed data cleaner formatting
#
#
# * Display a preview of the summary data frame
#
#

M_I = purchase_df['Item Name'].groupby(purchase_df['Item ID']).value_counts().sort_values(ascending= False)
M_I

type(M_I)

I_P = purchase_df['Price'].groupby(purchase_df['Item ID']).mean()
I_P

I_S = purchase_df['Price'].groupby(purchase_df['Item ID']).sum().sort_values(ascending= False)
I_S

M_P_I =pd.DataFrame({'Purchase': M_I,
                    'Item Price': (0),
                    'Total Purchase Value': (0)})
M_P_I.head()
#help
#75%

# ## Most Profitable Items

# * Sort the above table by total purchase value in descending order
#
#
# * Optional: give the displayed data cleaner formatting
#
#
# * Display a preview of the data frame
#
#

I_S.sort_values(ascending= False)

M_I

PO = purchase_df['Item ID'].groupby(purchase_df['Price']).value_counts()
PO.sort_values(ascending= False)

PI = purchase_df['Price'].groupby(purchase_df['Item Name']).sum().map('${:,.2f}'.format)
PI.sort_values(ascending= False)

# +
Profit= pd.DataFrame({'Purchase Count': (0),
                     'Item Price': I_P.map('${:,.2f}'.format),
                     'Total Purchase Value': I_S})

Profit.sort_values(by='Total Purchase Value', ascending= False).head(7)
#help
#80%

# + tags=[]
Summary = ("I notice that gaming is becoming a new sport and instead of going to colleges people are playing or developing games.", 
                 "Even though Male gamers out number Female gamers the average spending is higher for Females.",
                 "The top 5 most popular games were slightly different from the top 5 mosr profitable game.")
print(Summary)
#100%

StarcoderdataJupyterScriptsDedupFiltered