<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 = "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>total_num_players = len(purchase_data["SN"].value_counts())
total_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>Number_of_Unique_Items = len(purchase_data["Item Name"].unique())
Number_of_Unique_Items
Average_Price = "${:,.2f}".format(round(purchase_data["Price"].mean(),2))
Average_Price
Num_Purchases = purchase_data["Item Name"].count()
Num_Purchases
Total_Revenue = "${:,.2f}".format(purchase_data["Price"].sum())
Total_Revenue
Summary_dicts = [{
    "Number of Unique Items": Number_of_Unique_Items, 
    "Average Price": Average_Price,
    "Number of Purchases": Num_Purchases,
    "Total Revenue": Total_Revenue
}]
Summary_df = pd.DataFrame(Summary_dicts)
Summary_df<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>Female_df = purchase_data.loc[purchase_data["Gender"]=="Female",:]
Female_count = len(Female_df["SN"].value_counts())
Female_percent = "{:.2%}".format(Female_count/total_num_players)
Female_percent
Male_df = purchase_data.loc[purchase_data["Gender"]=="Male",:]
Male_count = len(Male_df["SN"].value_counts())
Male_percent = "{:.2%}".format(Male_count/total_num_players)
Male_percent
Other_df = purchase_data.loc[purchase_data["Gender"]=="Other / Non-Disclosed",:]
Other_count = len(Other_df["SN"].value_counts())
Other_percent = "{:.2%}".format(Other_count/total_num_players)
Other_percent
gender_dm = purchase_data.groupby(by="Gender", as_index = False).agg({"SN":pd.Series.nunique})
gender_dm = gender_dm.rename(columns={"SN":"Total Count"})
gender_dm["Percentage of Players"]=[Female_percent,Male_percent,Other_percent]
gender_dm<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>gender_unique_count = purchase_data.groupby("Gender")["SN"].nunique()
gender_unique_count
gender_purch = purchase_data.groupby(["Gender"])
gender_purch_count = gender_purch["Purchase ID"].count()
gender_purch_count
gender_purch_average = gender_purch["Price"].mean()
gender_purch_average
gender_purch_total = gender_purch["Price"].sum()
gender_purch_total
avg_purch = gender_purch_total/gender_unique_count
avg_purch
gender_purch_analysis = pd.DataFrame({"Purchase Count":gender_purch_count,"Average Purchase Price":gender_purch_average,"Total Purchase Value":gender_purch_total,"Avg Total Purchase per Person":avg_purch})
gender_purch_analysis["Average Purchase Price"]= gender_purch_analysis["Average Purchase Price"].map("${:,.2f}".format)
gender_purch_analysis["Total Purchase Value"]= gender_purch_analysis["Total Purchase Value"].map("${:,.2f}".format)
gender_purch_analysis["Avg Total Purchase per Person"]= gender_purch_analysis["Avg Total Purchase per Person"].map("${:,.2f}".format)
gender_purch_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>bins = [0,9,14,19,24,29,34,39,100]
group_names = ["<10","10-14","15-19","20-24","25-29","30-34","35-39","40+"]
age_data = pd.DataFrame(purchase_data)
age_data["Age Groups"] = pd.cut(purchase_data["Age"],bins,labels = group_names)
age_data
age_unique_count = age_data.groupby("Age Groups")["SN"].nunique()
age_unique_count
age_percent = age_unique_count/total_num_players*100
age_percent
age_demo = pd.DataFrame({"Total Count":age_unique_count,"Percentage of Players":age_percent})
age_demo["Percentage of Players"]= age_demo["Percentage of Players"].map("{:,.2f}%".format)
age_demo<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>age_purch_count = age_data.groupby("Age Groups")["Item Name"].count()
age_purch_count
age_purch_average = age_data.groupby("Age Groups")["Price"].mean()
age_purch_average
age_purch_total = age_data.groupby("Age Groups")["Price"].sum()
age_purch_total
age_purch = age_purch_total/age_unique_count
age_purch
age_purch_summary = pd.DataFrame({"Purchase Count":age_purch_count,"Average Purchase Price":age_purch_average,"Total Purchase Value":age_purch_total,"Avg Total Purchase per Person":age_purch})
age_purch_summary["Average Purchase Price"]= age_purch_summary["Average Purchase Price"].map("${:,.2f}".format)
age_purch_summary["Total Purchase Value"]= age_purch_summary["Total Purchase Value"].map("${:,.2f}".format)
age_purch_summary["Avg Total Purchase per Person"]= age_purch_summary["Avg Total Purchase per Person"].map("${:,.2f}".format)
age_purch_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>top_spenders_count = purchase_data.groupby("SN")["Item Name"].count()
top_spenders_count
top_spenders_purch_total = purchase_data.groupby("SN")["Price"].sum()
top_spenders_purch_total
top_spenders_purch_average = top_spenders_purch_total/top_spenders_count
top_spenders_purch_average
top_spenders_summary=pd.DataFrame({"Purchase Count":top_spenders_count,"Average Purchase Price":top_spenders_purch_average,"Total Purchase Value":top_spenders_purch_total})
top_spenders_summary = top_spenders_summary.sort_values("Total Purchase Value",ascending=False)
top_spenders_summary["Average Purchase Price"]= top_spenders_summary["Average Purchase Price"].map("${:,.2f}".format)
top_spenders_summary["Total Purchase Value"]= top_spenders_summary["Total Purchase Value"].map("${:,.2f}".format)
top_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>pop = purchase_data[["Item ID","Item Name","Price"]]
pop2 = pop.groupby(["Item ID","Item Name"])
item_purch_count = pop2["Price"].count()
item_price = pop2["Price"].mean()
item_purch_total = pop2["Price"].sum()
pop3 = pd.DataFrame({"Purchase Count":item_purch_count,"Item Price":item_price,"Total Purchase Value":item_purch_total})
pop3 = pop3.sort_values("Purchase Count",ascending=False)
pop3["Item Price"]= pop3["Item Price"].map("${:,.2f}".format)
pop3["Total Purchase Value"]= pop3["Total Purchase Value"].map("${:,.2f}".format)
pop3<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>pop4 = pd.DataFrame({"Purchase Count":item_purch_count,"Item Price":item_price,"Total Purchase Value":item_purch_total})
pop4 = pop3.sort_values("Total Purchase Value",ascending=False)
pop4["Item Price"]= pop4["Item Price"].map("${:,.2f}".format)
pop4["Total Purchase Value"]= pop3["Total Purchase Value"].map("${:,.2f}".format)
pop4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12097782831154065, 'fraction_numerical': 0.009891984081864695, 'mean_word_length': 8.927765237020315, 'pattern_counts': {'":': 25, '<': 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': '580660', 'license': 'Apache-2.0', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2722, 'n_words': 668, 'path': 'HerosOfPymoli/HeroesOfPymoli_LL.ipynb', 'repo_name': 'labulel/Pandas-Challenge', 'size': '8795'}