<jupyter_start><jupyter_text>Heroes Of Pymoli Data Analysis* Of the 1163 active players, the vast majority are male (84%). There also exists, a smaller, but notable proportion of female players (14%).* Our peak age demographic falls between 20-24 (44.8%) with secondary groups falling between 15-19 (18.60%) and 25-29 (13.4%).  -----  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.<jupyter_code># Dependencies and Setup
import pandas as pd
import pathlib

# File to Load (Remember to Change These)
file_to_load = pathlib.Path("Resources/purchase_data.csv")

# Read Purchasing File and store into Pandas data frame
purchase_data = pd.read_csv(file_to_load)
purchase_data<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code># Calculate total players in the data frame
total_players = len(purchase_data["SN"].unique())

# Store information about total players into a data frame
total_players_df = pd.DataFrame({"Total Players":[total_players]})

# Display the data frame
total_players_df<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Calculate number of unique items
total_items = len(purchase_data["Item Name"].unique())

# Calculate average price
average_price = round(purchase_data["Price"].mean(), 2)

# Calculate number of purchases 
purchase_number = len(purchase_data)

# Calculate total revenue
total_rev = purchase_data["Price"].sum()

# Store all the information in a data frame
purchase_analysis = pd.DataFrame(
    {"Number of Unique Items":[total_items],"Average Price":[average_price], 
     "Number of Purchases": [purchase_number], "Total Revenue": [total_rev]}
)

# Format the data frame
purchase_analysis["Average Price"] = purchase_analysis["Average Price"].map("${:,.2f}".format)
purchase_analysis["Total Revenue"] = purchase_analysis["Total Revenue"].map("${:,.2f}".format)


# Display the data frame
purchase_analysis<jupyter_output><empty_output><jupyter_text>Gender Demographics * Percentage and Count of Male Players* Percentage and Count of Female Players* Percentage and Count of Other / Non-Disclosed<jupyter_code># Create a data frame that shows how many players are in each gender
groupby_gender = purchase_data.groupby(['Gender'])
gender = groupby_gender['SN'].nunique()

# Store number of players in each gender into variables
total_female = gender["Female"]
total_male = gender["Male"]
total_other = gender["Other / Non-Disclosed"]

# Calculate percentages of player in each gender
female_percent = total_female / total_players * 100
male_percent = total_male / total_players * 100
other_percent = total_other / total_players * 100

# Store all the information in a data frame
Gender_demographics = pd.DataFrame(
    {"Total Count": [total_male, total_female, total_other], 
     "Percentage of Players": [male_percent, female_percent, other_percent]}, 
    index= ["Male", "Female", "Other / Non-Disclosed"] 
)

# Format the data frame
Gender_demographics["Percentage of Players"] = Gender_demographics["Percentage of Players"].map("{:,.2f}%".format)

# Display the data frame
Gender_demographics<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Create three different data frame to store information for each gender
female_df = purchase_data.loc[purchase_data["Gender"]=="Female",:]
male_df = purchase_data.loc[purchase_data["Gender"]=="Male",:]
other_df = purchase_data.loc[purchase_data["Gender"]=="Other / Non-Disclosed",:]

# Store total purchases each gender made in three different variables
purchase_count_f = len(female_df)
purchase_count_m = len(male_df)
purchase_count_o = len(other_df)

# calculate average purchases prices for each gender
average_purchase_price_f = female_df["Price"].mean()
average_purchase_price_m = male_df["Price"].mean()
average_purchase_price_o = other_df["Price"].mean()

# calculate total purchases value each gender made
total_purchase_value_f = female_df["Price"].sum()
total_purchase_value_m = male_df["Price"].sum()
total_purchase_value_o = other_df["Price"].sum()

# calculate average purchases each gender made
avg_purchase_f = total_purchase_value_f / total_female 
avg_purchase_m = total_purchase_value_m / total_male 
avg_purchase_o = total_purchase_value_o / total_other

# Store all the information in a data frame
purchasing_analysis = pd.DataFrame(
    {"Purchase Count": [purchase_count_f, purchase_count_m, purchase_count_o],
    "Average Purchase Price": [average_purchase_price_f, average_purchase_price_m, average_purchase_price_o],
    "Total Purchase Value": [total_purchase_value_f, total_purchase_value_m, total_purchase_value_o],
    "Avg Total Purchase per Person": [avg_purchase_f, avg_purchase_m, avg_purchase_o]}, 
    index = ["Female", "Male", "Other / Non-Disclosed"]
)

# Format the data frame
purchasing_analysis.index.name = "Gender"
purchasing_analysis["Average Purchase Price"] = purchasing_analysis["Average Purchase Price"].map("${:,.2f}".format)
purchasing_analysis["Total Purchase Value"] = purchasing_analysis["Total Purchase Value"].map("${:,.2f}".format)
purchasing_analysis["Avg Total Purchase per Person"] = purchasing_analysis["Avg Total Purchase per Person"].map("${:,.2f}".format)

# Display the data frame
purchasing_analysis<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Create bins and labels for bin
age_bin = [0, 9.9, 14.9, 19.9, 24.9, 29.9, 34.9, 39.9, 999999]
group_name = ["<10", "10-14","15-19", "20-24", "25-29", "30-34", "35-39", "40+"]

# Create the bins to group different age ranges
purchase_data["Age Range"] = pd.cut(x=purchase_data["Age"], bins = age_bin, labels = group_name)

# Create a data frame with number of players in each age range
age_groupby = purchase_data.groupby("Age Range")
age_data = age_groupby["SN"].nunique()

# calculate the percentage of player in each age range
players_percentage = age_data / total_players * 100

# Store all the information in a data frame
age_demographics = pd.DataFrame({"Total Count":age_data, "Percentage of Players":players_percentage})

# Format the data frame
age_demographics["Percentage of Players"] = age_demographics["Percentage of Players"].map("{:,.2f}%".format)

# Display the data frame
age_demographics<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Calculate number of purchases made in each age range
purchase_count = purchase_data["Age Range"].value_counts()

# Calculate total value of purchases made in each age range
total_value = age_groupby["Price"].sum()

# Calculate average purchase price in each age range
avg_purchase_price = total_value / purchase_count

# Calcuate average purchase per person in each age range
avg_purchase_per_person = total_value / age_data

# Store all the information in a data frame
age_purchasing_analysis = pd.DataFrame(
    {"Purchase Count": purchase_count, "Average Purchase Price": avg_purchase_price, 
     "Total Purchase Value": total_value, "Avg Total Purchase per Person": avg_purchase_per_person},
    index = group_name
)

# Format the data frame
age_purchasing_analysis["Average Purchase Price"] = age_purchasing_analysis["Average Purchase Price"].map("${:,.2f}".format)
age_purchasing_analysis["Total Purchase Value"] = age_purchasing_analysis["Total Purchase Value"].map("${:,.2f}".format)
age_purchasing_analysis["Avg Total Purchase per Person"] = age_purchasing_analysis["Avg Total Purchase per Person"].map("${:,.2f}".format)
age_purchasing_analysis.index.name = "Age Ranges"

# Display the data frame
age_purchasing_analysis<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Create a data frame to separate the data into fields accordint to "SN" 
sn_df = purchase_data.groupby("SN")

# Calculate number of purchases each player made
purchase_count = sn_df["SN"].count()

# Calculate total number of purchases each player made
total_purchase_value = sn_df["Price"].sum()

# Calculate average purchase price each player made
avg_purchase_price = total_purchase_value / purchase_count

# Store all the information in a data frame
top_spender = pd.DataFrame(
    {"Purchase Count":purchase_count, "Average Purchase Price": avg_purchase_price, 
     "Total Purchase Value": total_purchase_value}
)

# Format and sort the data frame
top_spender = top_spender.sort_values("Total Purchase Value", ascending = False)
top_spender["Average Purchase Price"] = top_spender["Average Purchase Price"].map("${:,.2f}".format)
top_spender["Total Purchase Value"] = top_spender["Total Purchase Value"].map("${:,.2f}".format)

# Display the data frame
top_spender.head()<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Create a data frame that separate the data into fields according to "Item ID" and "Item Name"
id_df = purchase_data.groupby(["Item ID", "Item Name"])

# Calculate how many times each item has been sold
purchase_count = id_df["Item ID"].count()

# Calculate total purchase value for each item
total_purchase_value = id_df["Price"].sum()

# Calculate each item's price
item_price = total_purchase_value / purchase_count

# Store all the information in a data frame
most_popular_items1 = pd.DataFrame(
    {"Purchase Count": purchase_count, "Item Price": item_price, 
     "Total Purchase Value": total_purchase_value}, 
)

# Format and sort the data frame
most_popular_items = most_popular_items1.sort_values("Purchase Count", ascending = False)
most_popular_items["Item Price"] = most_popular_items["Item Price"].map("${:,.2f}".format)
most_popular_items["Total Purchase Value"] = most_popular_items["Total Purchase Value"].map("${:,.2f}".format)

# Display the data frame
most_popular_items.head()<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Sort the data frame so it will show the item with highest total purchase value first
most_profitable_items = most_popular_items1.sort_values(["Total Purchase Value"], ascending = False)

# Format the data frame
most_profitable_items["Item Price"] = most_profitable_items["Item Price"].map("${:,.2f}".format)
most_profitable_items["Total Purchase Value"] = most_profitable_items["Total Purchase Value"].map("${:,.2f}".format)

# Display the data frame
most_profitable_items.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09805841083374123, 'fraction_numerical': 0.009544787077826725, 'mean_word_length': 6.358343337334934, 'pattern_counts': {'":': 23, '<': 42, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '296744', 'license': 'Apache-2.0', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3519, 'n_words': 1205, 'path': 'HeroesOfPymoli/HeroesOfPymoli_starter.ipynb', 'repo_name': 'beiniycitiznet/Pandas-Challenge', 'size': '12258'}