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

# Raw data file
file_to_load = "Resources/purchase_data.csv"

# Read purchasing file and store into pandas data frame
data = pd.read_csv(file_to_load)
data.head(2)<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code>total_players = len(data["SN"].unique())
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_items = len(data["Item ID"].unique())
unique_items
avg_price = round(data["Price"].mean(),2)
avg_price
total_rev = data["Price"].sum()
total_rev
total_purchases = data["Item ID"].count()
total_purchases
summary = pd.DataFrame({
                        "Number of Unique Items":[unique_items],
                        "Average Price":[avg_price],
                        "Number of Purchases":[total_purchases],
                        "Total Revenue":[total_rev]
                       })
summary<jupyter_output><empty_output><jupyter_text>Gender Demographics * 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>players = pd.DataFrame(data[["SN","Gender"]].drop_duplicates("SN"))
players = pd.DataFrame(players.groupby("Gender")["Gender"].count())
players = players.rename(columns={"Gender":"Players by Gender"})
players
players["Percentage of Players"] = players["Players by Gender"] / total_players
players["Percentage of Players"] = players["Percentage of Players"].map("{0:.2f}%".format)
players<jupyter_output><empty_output><jupyter_text>Purchasing Analysis (Gender) * Run basic calculations to obtain purchase count, avg. purchase price, etc. by gender* For normalized purchasing, divide total purchase value by purchase count, 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>by_gender_avg_price = pd.DataFrame(data.groupby("Gender")["Price"].mean())
by_gender_avg_price["Price"] = by_gender_avg_price["Price"].map("${:.2f}".format)
by_gender_avg_price = by_gender_avg_price.rename(columns={"Price":"Avgerage Purchase Price"})

by_gender_avg_price
by_gender_item_count = pd.DataFrame(data.groupby("Gender")["Item Name"].count()).rename(columns={"Item Name":"Purchase Count"})
by_gender_item_count
by_gender_total_value = pd.DataFrame(data.groupby("Gender")["Price"].sum())
by_gender_total_value = by_gender_total_value.rename(columns = {"Price":"Total Purchase Value"})
by_gender_total_value["Total Purchase Value"] = by_gender_total_value["Total Purchase Value"].map("${:.2f}".format)
by_gender_total_value
#Normalized Purchasing = Purchase Price / Purchase Count
df = pd.merge(pd.merge(by_gender_item_count, by_gender_avg_price, on="Gender"),by_gender_total_value, on="Gender")
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># Establish bins for ages
age_bins = [0, 9.90, 14.90, 19.90, 24.90, 29.90, 34.90, 39.90, 99999]
group_names = ["<10", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40+"]<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, etc. in the table below* Calculate Normalized Purchasing* Create a summary data frame to hold the results* Optional: give the displayed data cleaner formatting* Display the summary data frame<jupyter_code>age_table = data[["SN","Age","Price"]]
age_table.head(2)
bins = [0, 9.9, 14.9, 19.9, 24.9, 29.9, 34.9, 39.9, 99.9]
age_groups = ["<10","10-14","15-19","20-24","25-29","30-34","35-39","40+"]
age_table["Age Group"] = pd.cut(age_table["Age"], bins, labels = age_groups)
age_table.head(2)
by_age_avg_price = pd.DataFrame(age_table.groupby("Age Group")["Price"].mean())
by_age_avg_price = by_age_avg_price.rename(columns={"Price":"Average Purchase Price"})
by_age_avg_price["Average Purchase Price"] = by_age_avg_price["Average Purchase Price"].map("${:.2f}".format)
by_age_avg_price.head()
by_age_purchase_count = pd.DataFrame(age_table.groupby("Age Group")["SN"].count())
by_age_purchase_count = by_age_purchase_count.rename(columns={"SN":"Total Purchases"})
by_age_purchase_count.head()
by_age_total_purchase = pd.DataFrame(age_table.groupby("Age Group")["Price"].sum())
by_age_total_purchase = pd.DataFrame(by_age_total_purchase.rename(columns={"Price":"Total Purchase Value"}))
by_age_total_purchase = pd.DataFrame(by_age_total_purchase["Total Purchase Value"].map("${:.2f}".format))
by_age_total_purchase.head()
by_age_summary = pd.merge(pd.merge(by_age_purchase_count, by_age_avg_price, on="Age Group"), by_age_total_purchase, on="Age Group")
by_age_summary<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>by_spenders_top_5 = pd.DataFrame(data.groupby("SN")["Price"].sum())
by_spenders_top_5 = pd.DataFrame(by_spenders_top_5.sort_values(by="Price", ascending=False).head(5))
by_spenders_top_5 = pd.DataFrame(by_spenders_top_5["Price"].map("${:.2f}".format))
by_spenders_top_5 = pd.DataFrame(by_spenders_top_5.rename(columns = {"Price":"Total Purchase Value"}))
by_spenders_top_5
by_spenders_purchase_count = pd.DataFrame(data.groupby("SN")["Item Name"].count())
by_spenders_purchase_count = by_spenders_purchase_count.sort_values(by="Item Name", ascending=False)
by_spenders_purchase_count.head(2)
by_spenders_avg_price = pd.DataFrame(data.groupby("SN")["Price"].mean())
by_spenders_avg_price = pd.DataFrame(by_spenders_avg_price["Price"].map("${:.2f}".format))
by_spenders_avg_price = pd.DataFrame(by_spenders_avg_price.rename(columns = {"Price":"Average Purchase Price"}))
by_spenders_avg_price.head(2)
by_spenders_summary = pd.merge(pd.merge(by_spenders_purchase_count, by_spenders_avg_price, on="SN"), by_spenders_top_5, on="SN")
by_spenders_summary<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>popularity_table = data.groupby(["Item ID", "Item Name"])
popularity_count = popularity_table.count().sort_values(by="Purchase ID", ascending=False).head(5)
popularity_count = pd.DataFrame(popularity_count.rename(columns={"Purchase ID":"Purchase Count"})["Purchase Count"])
popularity_total_price = pd.DataFrame(popularity_table["Price"].sum())
popularity_total_price = pd.DataFrame(popularity_total_price["Price"].map("${:.2f}".format))
popularity_total_price = pd.DataFrame(popularity_total_price.rename(columns={"Price":"Total Purchase Price"}))
popularity_item_price = pd.DataFrame(popularity_table["Price"].unique())
popularity_item_price = pd.DataFrame(popularity_item_price.rename(columns={"Price":"Item Price"}))
popularity_merged = pd.merge(pd.merge(popularity_count, popularity_item_price, on=["Item ID","Item Name"]),
                 popularity_total_price, on=["Item ID","Item Name"])
popularity_merged<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1267345050878816, 'fraction_numerical': 0.01954209065679926, 'mean_word_length': 7.136406396989652, 'pattern_counts': {'":': 16, '<': 39, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '19451', 'license': 'MIT', 'n_tokens_mistral': 2892, 'n_tokens_neox': 2692, 'n_words': 678, 'path': 'HeroesOfPymoli_starter.ipynb', 'repo_name': 'pedrovasquez16/Pandas', 'size': '8648'}