<jupyter_start><jupyter_text>Heroes of Pymoli Analysis<jupyter_code>#Import Pandas Library
import pandas as pd

#Reference csv file purchasedata
csv_path = "./purchase_data.csv"

#Read csv into panda data frame
pymoli_purchase_df = pd.read_csv(csv_path)
pymoli_purchase_df.head()<jupyter_output><empty_output><jupyter_text>Total Number of Players<jupyter_code>#Find the total number of players
player_count = len(pymoli_purchase_df["SN"].unique())
player_count<jupyter_output><empty_output><jupyter_text>Purchasing Analysis Summary Table<jupyter_code>#Create a table for Purchasing Analysis by finding the number of unique items, 
##average purchase price, total number of purchases, and total revenue

#Find number of unique items
unique_item_count = len(pymoli_purchase_df["Item ID"].unique())
unique_item_count

#Find the Average Purchase Price
avg_purchase_price = "${:.2f}".format(pymoli_purchase_df["Price"].mean())
avg_purchase_price

#Find Total Number of Purchases
total_purchases = pymoli_purchase_df["Purchase ID"].count()
total_purchases

#Find the Total Revenue
total_revenue = "${0:,.2f}".format(pymoli_purchase_df["Price"].sum())
total_revenue

#Create a summary table
#columns = ["Number of Unique Items", "Average Purchase Price", "Total Number of Purchases", "Total Revenue"]
purchasing_analysis_df = pd.DataFrame({"Number of Unique Items": [unique_item_count],
                              "Average Purchase Price": [avg_purchase_price],
                              "Total Number of Purchases": [total_purchases],
                              "Total Revenue": [total_revenue]})
purchasing_analysis_df<jupyter_output><empty_output><jupyter_text>Gender Demographics Table<jupyter_code>#Find the count and percentage of male, female and Other/non-disclosed players

#Find the count and percentage of male players
male_df = pymoli_purchase_df[pymoli_purchase_df["Gender"] == "Male"]
male_count = len(male_df["SN"].unique())
male_count
male_percentage = "{:.2%}".format(male_count/player_count)
male_percentage

#Find the count and percentage of male players
female_df = pymoli_purchase_df[pymoli_purchase_df["Gender"] == "Female"]
female_count = len(female_df["SN"].unique())
female_count
female_percentage = "{:.2%}".format(female_count/player_count)
female_percentage

#Find the count and percentage of other/non-disclosed players
other_df = pymoli_purchase_df[pymoli_purchase_df["Gender"] == "Other / Non-Disclosed"]
other_count = len(other_df["SN"].unique())
other_count
other_percentage = "{:.2%}".format(other_count/player_count)
other_percentage

#Create a summary table based on gender
gender_analysis_df = pd.DataFrame({
                              "Total Count": [male_count, female_count, other_count],
                              "Percentage of Players": [male_percentage, female_percentage, other_percentage]},
                                index = ["Male", "Female", "Other / Non-Disclosed"])
gender_analysis_df<jupyter_output><empty_output><jupyter_text>Purchasing Analysis by Gender<jupyter_code>#Find purchasing trends by each gender category for purchase count, average purchase price,
##total purchase value, and average purchase total per gender

#Find Male purchase count, average purchase price, total purchase value and average purchase total
male_purchase_count = len(male_df["Purchase ID"].unique())
male_purchase_count
male_purchase_price = (male_df["Price"].sum())
male_purchase_price
male_avg_purchase = "${:.2f}".format(male_purchase_price/male_purchase_count)
male_avg_purchase
male_purchase_price = "${0:,.2f}".format(male_df["Price"].sum())
male_purchase_price
male_individ_price = male_df.groupby("SN")["Price"].sum()
avg_per_male = "${:.2f}".format(male_individ_price.mean())
avg_per_male

#Find Female purchase count, average purchase price, total purchase value and average purchase total
female_purchase_count = len(female_df["Purchase ID"].unique())
female_purchase_count
female_purchase_price = (female_df["Price"].sum())
female_purchase_price
female_avg_purchase = "${:.2f}".format(female_purchase_price/female_purchase_count)
female_avg_purchase
female_purchase_price = "${0:,.2f}".format(female_df["Price"].sum())
female_purchase_price
female_individ_price = female_df.groupby("SN")["Price"].sum()
avg_per_female = "${:.2f}".format(female_individ_price.mean())
avg_per_female

#Find Other purchase count, average purchase price, total purchase value and average purchase total
other_purchase_count = len(other_df["Purchase ID"].unique())
other_purchase_count
other_purchase_price = (other_df["Price"].sum())
other_purchase_price
other_avg_purchase = "${:.2f}".format(other_purchase_price/other_purchase_count)
other_avg_purchase
other_purchase_price = "${0:,.2f}".format(other_df["Price"].sum())
other_purchase_price
other_individ_price = other_df.groupby("SN")["Price"].sum()
avg_per_other = "${:.2f}".format(other_individ_price.mean())
avg_per_other

#Create a summary table of purchasing analysis by gender
gender_purchase_df = pd.DataFrame({
                              "Gender": ["Male", "Female", "Other / Non-Disclosed"],
                              "Purchase Count": [male_purchase_count, female_purchase_count, other_purchase_count],
                              "Average Purchase Price": [male_avg_purchase, female_avg_purchase, other_avg_purchase],
                              "Total Purchase Value": [male_purchase_price, female_purchase_price, other_purchase_price],
                              "Total Purchase per Person": [avg_per_male, avg_per_female, avg_per_other]
                                })
gender_purchase_df<jupyter_output><empty_output><jupyter_text>Age Demographics<jupyter_code>#Break down of purchase count, average purchase price, total purchase value and average purchase total by age group
##Year categories by bins of 4 years

#Find max and min age for year categories
max_age = pymoli_purchase_df["Age"].max()
max_age
min_age = pymoli_purchase_df["Age"].min()
min_age

#Create a bin for age groups
bins = [0, 10, 14, 19, 24, 29, 34, 39, 44, 49]
#Create names for each group
age_groups = ["<10", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49" ]


pymoli_purchase_df["Age Summary"] = pd.cut(pymoli_purchase_df["Age"], bins, labels=age_groups, include_lowest=True)
pymoli_purchase_df.head()
#Find purchase count, average purchase price, total purchase value and average purchase total by age group
age_group_count = pymoli_purchase_df.groupby("Age Summary")["SN"].nunique()
age_group_count
#Find purchase count per age group
purchase_count_age = pymoli_purchase_df.groupby("Age Summary")["Price"].count()
purchase_count_age
avg_purchase_age = pymoli_purchase_df.groupby("Age Summary")["Price"].mean()
avg_purchase_age
total_purchase_age = pymoli_purchase_df.groupby("Age Summary")["Price"].sum()
total_purchase_age
avg_total_age = total_purchase_age/age_group_count
avg_total_age

#Format dollar vaules and round to 2 decimals
age_purchase_df = pd.DataFrame({
            "Purchase Count" : purchase_count_age,
             "Average Purchase Price": avg_purchase_age.map("${:.2f}".format),
            "Total Purchase Value": total_purchase_age.map("${:,.2f}".format),
            "Average Total Purchase per Person": avg_total_age.map("${:.2f}".format)})
age_purchase_df<jupyter_output><empty_output><jupyter_text>Top Spenders<jupyter_code>#Identify the top 5 spenders by total purchase value and list in table with SN, purchase count, average
##purchase price and total purchase value

top_purchase_count = pymoli_purchase_df.groupby("SN")["Price"].count()
top_avg_purchase = pymoli_purchase_df.groupby("SN")["Price"].mean()
top_total_purchase = pymoli_purchase_df.groupby("SN")["Price"].sum()

#Create table for top purchases
top_player_purchase_df = pd.DataFrame({
    "Purchase Count": top_purchase_count, 
    "Average Purchase Price": top_avg_purchase.map("${:.2f}".format),
    "Total Purchase Value": top_total_purchase.map("${:.2f}".format)})
top_player_purchase_df

#Sort by top 5
top_player_purchase_sort = top_player_purchase_df.sort_values("Total Purchase Value", ascending=False)
top_player_purchase_sort.head()<jupyter_output><empty_output><jupyter_text>Most Popular Items<jupyter_code>#Find top 5 items by purchase count
##List by Item ID, Item Name, Purchase Count, Item Price, and Total Purchase Value
top_item_df = pymoli_purchase_df[["Item ID", "Item Name", "Price"]]

#Find item count, item price and total purchse value
item_count = top_item_df.groupby(["Item ID", "Item Name"])["Price"].count()
item_price = top_item_df.groupby(["Item ID", "Item Name"])["Price"].mean()
total_item_value = top_item_df.groupby(["Item ID", "Item Name"])["Price"].sum()

#Place in table
top_item_summary_df = pd.DataFrame({
    "Purchase Count": item_count,
    "Item Price": item_price,
    "Total Purchase Value": total_item_value})
top_item_summary_df

#Sort for top 5 items and format values
top_item_sorted = top_item_summary_df.sort_values("Purchase Count", ascending=False)
top_item_sorted
top_item_sorted["Item Price"] = top_item_sorted["Item Price"].map("${:.2f}".format)
top_item_sorted["Total Purchase Value"] = top_item_sorted["Total Purchase Value"].map("${:.2f}".format)
top_item_sorted.head()<jupyter_output><empty_output><jupyter_text>Most Profitable Items<jupyter_code>#Find the top 5 most profitable items by total purchase value and list in table
##List by Item ID, Item Name, Purchase Count, Item Price, and Total Purchase Value
top_profit_df = top_item_summary_df.sort_values("Total Purchase Value", ascending=False)

#Format Item Price and Total Purchase Price
top_profit_df["Item Price"] = top_profit_df["Item Price"].map("${:.2f}".format)
top_profit_df["Total Purchase Value"] = top_profit_df["Total Purchase Value"].map("${:.2f}".format)
top_profit_df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11865611043442956, 'fraction_numerical': 0.008830694275274055, 'mean_word_length': 5.933849401829697, 'pattern_counts': {'":': 20, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '171330', 'license': 'MIT', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3150, 'n_words': 733, 'path': 'HeroesOfPymoli/Heroes of Pymoli.ipynb', 'repo_name': 'akrakowsky/pandas-challenge', 'size': '9852'}