# ---
# 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
# ---

import os
import pandas as pd
import numpy as np

HOP_csv_path = "HeroesOfPymoli.csv"

purchase_data_df = pd.read_csv(HOP_csv_path)
purchase_data_df.head()

total_players = len(purchase_data_df["SN"].value_counts())

player_count = pd.DataFrame({"Total Players":[total_players]})
player_count

unique_items = purchase_data_df['Item ID'].nunique()


average_price = purchase_data_df['Price'].mean()

number_of_purchases = purchase_data_df['Purchase ID'].count()

total_revenue = purchase_data_df['Price'].sum()

# +
purchasing_analysis_df = pd.DataFrame({'Number of Unique Items': [unique_items],
                                      "Average Price":[average_price],
                                      "Number of Purchases":[number_of_purchases],
                                      "Total Revenue": [total_revenue]})

purchasing_analysis_df

purchasing_analysis_df.style.format({"Average Price":"${:,.2f}",
                                    "Total Revenue":"${:,.2f}"})

# -

gender_group = purchase_data_df.groupby("Gender")

total_gender = gender_group.nunique()["SN"]

player_percentage = total_gender/total_players * 100

gender_demographics = pd.DataFrame({"Total Count": total_gender,
                                    "Percentage of Players": player_percentage,
                                   })

gender_demographics.index.name = None

gender_demographics.sort_values(["Total Count"], ascending = False).style.format({"Percentage of Players":"{:.2f}"})

purchase_count = gender_group["Purchase ID"].count()

avg_purchase_price = gender_group["Price"].mean()

avg_purchase_total = gender_group["Price"].sum()

avg_purchase_per_person = avg_purchase_total/total_gender

gender_demographics = pd.DataFrame({"Purchase Count": purchase_count, 
                                    "Average Purchase Price": avg_purchase_price,
                                    "Average Purchase Value":avg_purchase_total,
                                    "Avg Purchase Total per Person": avg_purchase_per_person})

gender_demographics.index.name = "Gender"

gender_demographics.style.format({"Average Purchase Value":"${:,.2f}",
                                  "Average Purchase Price":"${:,.2f}",
                                  "Avg Purchase Total per Person":"${:,.2f}"})

age_bins = [0, 9.90, 14.90,19.90, 24.90, 29.90, 34.90, 39.90, 99999]
age_group = ["<10", "10-14", "15-19", "20-24", "25-29", "30-34", "35-40", "40+"]

purchase_data_df["Age Group"] = pd.cut(purchase_data_df["Age"], age_bins, labels=age_group)
purchase_data_df

age_grouped = purchase_data_df.groupby("Age Group")

total_count_age = age_grouped["SN"].nunique()

age_percentage = (total_count_age/total_players)*100

age_demographics = pd.DataFrame({"Percentage of Players": age_percentage,
                                "Total Count": total_count_age})

age_demographics.index.name = None

age_demographics.style.format({"Percentage of Players":"{:,.2f}"})

purchase_count_age = age_grouped["Purchase ID"].count()

avg_purchase_price_age = age_grouped["Price"].mean()

total_purchase_value = age_grouped["Price"].sum()

avg_purchase_per_person_age = total_purchase_value/total_count_age

age_demographics = pd.DataFrame({"Purchase Count": purchase_count_age,
                                "Average Purchase Price": avg_purchase_price_age,
                                "Total Purchase Value": total_purchase_value,
                                "Average Purchase Total per Person": avg_purchase_per_person_age})

age_demographics.index.name = None

age_demographics.style.format({"Average Purchase Price":"${:,.2f}",
                              "Total Purchase Value":"${:,.2f}",
                              "Average Purchase Total per Person":"${:,.2f}"})

spenders = purchase_data_df.groupby("SN")

purchase_count_spender = spenders["Purchase ID"].count()

avg_purchase_price_spender = spenders["Price"].mean()

purchase_total_spender = spenders["Price"].sum()

top_spenders = pd.DataFrame({"Purchase Count": purchase_count_spender,
                             "Average Purchase Price": avg_purchase_price_spender,
                             "Total Purchase Value":purchase_total_spender})

spenders_format = top_spenders.sort_values(["Total Purchase Value"], ascending=False).head()

spenders_format.style.format({"Average Purchase Total":"${:,.2f}",
                                 "Average Purchase Price":"${:,.2f}", 
                                 "Total Purchase Value":"${:,.2f}"})

items = purchase_data_df[["Item ID", "Item Name", "Price"]]

item_info = items.groupby(["Item ID","Item Name"])

purchase_count_item = item_info["Price"].count()

purchase_value = (item_info["Price"].sum())

item_price = purchase_value/purchase_count_item

most_popular_items = pd.DataFrame({"Purchase Count": purchase_count_item, 
                                   "Item Price": item_price,
                                   "Total Purchase Value":purchase_value})

popular_format = most_popular_items.sort_values(["Purchase Count"], ascending=False).head()

popular_format.style.format({"Item Price":"${:,.2f}",
                                "Total Purchase Value":"${:,.2f}"})

popular_format = most_popular_items.sort_values(["Total Purchase Value"],
                                                   ascending=False).head()

popular_format.style.format({"Item Price":"${:,.2f}",
                                "Total Purchase Value":"${:,.2f}"})



StarcoderdataJupyterScriptsDedupFiltered