<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

# 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>player_demographics = purchase_data.loc[:, ["Gender","SN","Age"]]
player_demographics = player_demographics.drop_duplicates()
num_players = player_demographics.count()[0]
pd.DataFrame({"Total Players": [num_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>average_item_price = purchase_data["Price"].mean()
total_purchase_value = purchase_data["Price"].sum()
purchase_count = purchase_data["Price"].count()
item_count = len(purchase_data["Item ID"].unique())
summary_table = pd.DataFrame({
"Number of Unique Items": item_count,
"Total Revenue":[total_purchase_value],
"Number of Purchases":[purchase_count],
"Average Price": [average_item_price]

})
#summary_table = summary_table.round(2)
#summary_table["Average Price"] = summary_table["Average Price"].map("${:,.2f}".format)
#summary_table["Number of Purchases"] = summary_table["Number of Purchases"].map("{:,}".format)
#summary_table["Total Revenue"] = summary_table["Total Revenue"].map("${:,.2f}".format)
#summary_table = summary_table.loc[:,["Number of Unique Items","Average Price", "Number of Purchase"]]
#summary_table = summary_table.loc[:,["Number of Unique Items", "Average Price", "Number of Purchases", "Total Revenue"]]
#summary_table



summary_table = pd.DataFrame({
"Number of Unique Items": item_count,
"Total Revenue":[total_purchase_value],
"Number of Purchases":[purchase_count],
"Average Price": [average_item_price]

})
summary_table
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+"]
player_demographics["Age Ranges"] = pd.cut(player_demographics["Age"], age_bins,
                                           labels=group_names)

age_demographics_totals = player_demographics["Age Ranges"].value_counts()
age_demographics_percents = age_demographics_totals / num_players
age_demographics = pd.DataFrame({"Total Count": age_demographics_totals, "Percentage of Players": age_demographics_percents})


age_demographics["Percentage of Players"] = age_demographics["Percentage of Players"].map("{:,.2%}".format)
age_demographics

purchase_data["Age Ranges"] = pd.cut(purchase_data["Age"], age_bins, labels=group_names)

age_purchase_total = purchase_data.groupby(["Age Ranges"]).sum()["Price"].rename("Total purchase Value")
age_average = purchase_data.groupby(["Age Ranges"]).mean()["Price"].rename("Average purchase Price")
age_count = purchase_data.groupby(["Age Ranges"]).count()["Price"].rename("Purchase count")

normalized_total = age_purchase_total / age_demographics["Total Count"]

normalized_total
age_data = pd.DataFrame({"Purchase count" : age_count,
                        "Average Purchase Price": age_average,
                       "Total Purchase Value" : age_purchase_total,
                        "Normalized Total": normalized_total})

age_data["Average Purchase Price"] = age_data["Average Purchase Price"].map("${:,.2f}".format)
age_data["Total Purchase Value"] = age_data["Total Purchase Value"].map("${:,.2f}".format)
age_data["Purchase count"] = age_data["Purchase count"].map("{:,}".format)
age_data["Avg Total Purchase per Person"] = age_data["Normalized Total"].map("${:,.2f}".format)

age_data[["Purchase count","Average Purchase Price", "Total Purchase Value", "Avg Total Purchase per Person"]]

gender_demographics_total = player_demographics["Gender"].value_counts()
gender_demographics_percent = gender_demographics_total / num_players
gender_demographics = pd.DataFrame({
"Total Count": gender_demographics_total,
"Percentage of Players": gender_demographics_percent})
gender_demographics["Percentage of Players"] = gender_demographics["Percentage of Players"].map("{:,.2%}".format)
gender_demographics


player_demographics = purchase_data.loc[:, ["Gender","SN","Age"]]
player_demographics = player_demographics.drop_duplicates()
num_players = player_demographics.count()[0]
pd.DataFrame({"Total Players": [num_players]  })


gender_purchase_total= purchase_data.groupby(["Gender"]).sum()["Price"].rename("Total Purchase Value")
gender_average = purchase_data.groupby(["Gender"]).mean()["Price"].rename("Average Purchase Price")
gender_count = purchase_data.groupby(["Gender"]).count()["Price"].rename("Purchase Count")
normalized_total = gender_purchase_total / gender_demographics["Total Count"]
gender_data = pd.DataFrame({"Purchase Count":gender_count,
                          "Average Purchase Price": gender_average,
                           "Total Purchase Value": gender_purchase_total,
                          "Normalized Totals": normalized_total
                         
                         })
#gender_data
gender_data["Average Purchase Price"] = gender_data["Average Purchase Price"].map("${:,.2f}".format)
gender_data["Total Purchase Value"] = gender_data["Total Purchase Value"].map("${:,.2f}".format)
gender_data["Purchase Count"] = gender_data["Purchase Count"].map("{:,}".format)
gender_data["Avg Total Purchase per person"] = gender_data["Normalized Totals"].map("${:,.2f}".format)
gender_data[["Purchase Count","Average Purchase Price","Total Purchase Value", "Avg Total Purchase per person"]]<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>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+"]
player_demographics["Age Ranges"] = pd.cut(player_demographics["Age"], age_bins,
                                           labels=group_names)

age_demographics_totals = player_demographics["Age Ranges"].value_counts()
age_demographics_percents = age_demographics_totals / num_players
age_demographics = pd.DataFrame({"Total Count": age_demographics_totals, "Percentage of Players": age_demographics_percents})


age_demographics["Percentage of Players"] = age_demographics["Percentage of Players"].map("{:,.2%}".format)
age_demographics<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>user_total = purchase_data.groupby(["SN"]).sum()["Price"].rename("Total Purchase Value")
user_average = purchase_data.groupby(["SN"]).mean()["Price"].rename("Average Purchase Price")
user_count = purchase_data.groupby(["SN"]).count()["Price"].rename("Purchase Count")
user_data = pd.DataFrame({"Total Purchase Value": user_total,"Average Purchase Price": user_average,"Purchase Count": user_count})
user_sorted = user_data.sort_values("Total Purchase Value",ascending=False)
user_sorted<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>item_data = purchase_data[["Item ID", "Item Name", "Price"]]
total_item_purchase = item_data.groupby(["Item ID","Item Name", ]).sum()["Price"].rename("Total Purchase Price")
average_item_purchase = item_data.groupby(["Item ID","Item Name",]).mean()["Price"]
item_count = item_data.groupby(["Item ID", "Item Name"]).count()["Price"].rename("Purchase Count")


item_data_pd = pd.DataFrame({"Total Purchase Value":total_item_purchase,
                            "Item Price":average_item_purchase, 
                             "Purchase Count": item_count})

item_data_count_sorted = item_data_pd.sort_values("Purchase Count",ascending = False)

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

item_data_count_sorted[["Purchase Count", "Item Price", "Total Purchase Value"]].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>item_total_purchase = item_data_pd.sort_values("Total Purchase Value", ascending = False)
item_total_purchase ["Item Price"] = item_total_purchase["Item Price"].map("${:,.2f}".format)
item_total_purchase ["Purchase Count"] = item_total_purchase["Purchase Count"].map("{:,}".format)
item_total_purchase ["Total Purchase Value"] = item_total_purchase["Total Purchase Value"].map("${:,.2f}".format)


item_total_purchase[["Purchase Count", "Item Price", "Total Purchase Value"]].head(5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13738493516777195, 'fraction_numerical': 0.013857270117786796, 'mean_word_length': 6.300578034682081, 'pattern_counts': {'":': 28, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '436331', 'license': 'MIT', 'n_tokens_mistral': 3255, 'n_tokens_neox': 2995, 'n_words': 733, 'path': 'HeroesOfPymoli_Main submission .ipynb', 'repo_name': 'sbarimani/Pandas_Main', 'size': '10103'}