<jupyter_start><jupyter_text>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 numpy as np

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

# Read Purchasing File and store into Pandas data frame
purchase_data = pd.read_csv(file_to_load)

purchase_data.head()<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code>total_players = pd.DataFrame([{"Total Number of Players" : purchase_data["SN"].nunique()}])
total_players<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>unique = purchase_data["Item Name"].nunique()
avg_price = np.mean(purchase_data["Price"])
num_purchases = purchase_data["Item Name"].size
revenue = sum(purchase_data["Price"])
summary = pd.DataFrame([{"Number of Unique Items": unique, "Average Price": avg_price, "Number of Purchases": num_purchases,
                       "Total Revenue": revenue}])
summary["Average Price"] = summary["Average Price"].map("${:.2f}".format)
summary["Total Revenue"] = summary["Total Revenue"].map("${:,.2f}".format)
summary = summary[["Number of Unique Items", "Average Price", "Number of Purchases", "Total Revenue"]]
summary<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>demographics = purchase_data.groupby(['Gender'])
total_count = demographics["SN"].nunique()
percentage = (total_count/purchase_data["SN"].nunique()) * 100

summary_df = pd.DataFrame({"Total Count": total_count, "Percentage of Players": percentage})
summary_df["Percentage of Players"] = summary_df["Percentage of Players"].map("{:.2f}".format)
summary_df = summary_df[["Total Count", "Percentage of Players"]]
summary_df = summary_df.sort_values(by="Total Count", ascending = False)
summary_df<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>demographics = purchase_data.groupby(['Gender'])
total_count = demographics["SN"].nunique()
purchase_count = demographics["Purchase ID"].count()
avg_price = demographics["Price"].mean()
total_purchases = demographics["Price"].sum()
avg_per_person = total_purchases/total_count

summary_df = pd.DataFrame({"Purchase Count": purchase_count, "Average Purchase Price": avg_price, 
                           "Total Purchase Value": total_purchases, "Average Total Purchase per Person": avg_per_person})
summary_df["Average Purchase Price"] = summary_df["Average Purchase Price"].map("${:.2f}".format)
summary_df["Average Total Purchase per Person"] = summary_df["Average Total Purchase per Person"].map("${:.2f}".format)
summary_df["Total Purchase Value"] = summary_df["Total Purchase Value"].map("${:,.2f}".format)

summary_df = summary_df[["Purchase Count", "Average Purchase Price", "Total Purchase Value", 
                         "Average Total Purchase per Person"]]
summary_df<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>bins = [0, 9, 14, 19, 24, 29, 34, 39, 100]

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

purchase_data["Age Demographic"] = pd.cut(purchase_data["Age"], bins, labels = demographics)
age_demographics = purchase_data.groupby("Age Demographic")

total_count = age_demographics["SN"].nunique()
total_players = purchase_data["SN"].nunique()
percentage_of_players = (total_count/total_players) * 100

summary_df = pd.DataFrame({"Total Count": total_count, "Percentage of Players": percentage_of_players})
summary_df["Percentage of Players"] = summary_df["Percentage of Players"].map("{:.2f}".format)
summary_df = summary_df[["Total Count", "Percentage of Players"]]
summary_df<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>bins = [0, 9, 14, 19, 24, 29, 34, 39, 100]

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

purchase_data["Age Demographic"] = pd.cut(purchase_data["Age"], bins, labels = demographics)
age_demographics = purchase_data.groupby("Age Demographic")

purchase_count = age_demographics["Purchase ID"].count() #Number of purchases per demographic
avg_price = age_demographics["Price"].mean() #Price per demographic 
total_purchase_value = purchase_count * avg_price #Amount purchased per demographic 

avg_total_per_person = age_demographics["Price"].sum()/age_demographics["SN"].nunique()

summary_df = pd.DataFrame({"Purchase Count": purchase_count, "Average Purchase Price": avg_price,
                          "Total Purchase Value": total_purchase_value, "Average Total per Person": avg_total_per_person})

summary_df["Average Purchase Price"] = summary_df["Average Purchase Price"].map("${:.2f}".format)
summary_df["Average Total per Person"] = summary_df["Average Total per Person"].map("${:.2f}".format)
summary_df["Total Purchase Value"] = summary_df["Total Purchase Value"].map("${:.2f}".format)

summary_df = summary_df[["Purchase Count", "Average Purchase Price", "Total Purchase Value", 
                         "Average Total per Person"]]

summary_df<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>spending = purchase_data.groupby("SN")
num_purchases = spending["Purchase ID"].count()
total_purchase_value = spending["Price"].sum()
avg_price = total_purchase_value/num_purchases
summary_df = pd.DataFrame({"Purchase Count": num_purchases, "Average Purchase Price": avg_price, 
                           "Total Purchase Value": total_purchase_value})
summary_df = summary_df.sort_values(by="Total Purchase Value", ascending = False)

summary_df["Average Purchase Price"] = summary_df["Average Purchase Price"].map("${:.2f}".format)
summary_df["Total Purchase Value"] = summary_df["Total Purchase Value"].map("${:.2f}".format)

summary_df = summary_df[["Purchase Count", "Average Purchase Price", "Total Purchase Value"]]
summary_df.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>popular_items = purchase_data.groupby(["Item ID", "Item Name"])
purchase_count = popular_items["Item ID"].count()
item_price = popular_items["Price"].mean()
total_purchase_value = purchase_count * item_price 

summary_df = pd.DataFrame({"Purchase Count": purchase_count, "Item Price": item_price, 
                           "Total Purchase Value": total_purchase_value})
summary_df = summary_df.sort_values(by="Purchase Count", ascending = False)

summary_df["Item Price"] = summary_df["Item Price"].map("${:.2f}".format)
summary_df["Total Purchase Value"] = summary_df["Total Purchase Value"].map("${:.2f}".format)

summary_df = summary_df[["Purchase Count", "Item Price", "Total Purchase Value"]]
summary_df.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>popular_items = purchase_data.groupby(["Item ID", "Item Name"])
purchase_count = popular_items["Item ID"].count()
item_price = popular_items["Price"].mean()
total_purchase_value = purchase_count * item_price 


summary_df = pd.DataFrame({"Purchase Count": purchase_count, "Item Price": item_price, 
                           "Total Purchase Value": total_purchase_value})
summary_df = summary_df.sort_values(by="Total Purchase Value", ascending = False)

summary_df["Item Price"] = summary_df["Item Price"].map("${:.2f}".format)
summary_df["Total Purchase Value"] = summary_df["Total Purchase Value"].map("${:.2f}".format)

summary_df = summary_df[["Purchase Count", "Item Price", "Total Purchase Value"]]
summary_df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12293119600291454, 'fraction_numerical': 0.011658165920682835, 'mean_word_length': 6.289833080424886, 'pattern_counts': {'":': 25, '<': 43, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '200296', 'license': 'ADSL', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2744, 'n_words': 841, 'path': 'HeroesOfPymoli_starter.ipynb', 'repo_name': 'VHarv/VHarv-Pandas-Challenge', 'size': '9607'}