<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

# 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)
# Display a few elements of the file stored in a Data frame
purchase_data.head()
# Display a statistical overview of the Dataframe
purchase_data.describe()<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code>player_count=len(purchase_data["SN"].unique())
player_count_display=pd.DataFrame({"Total Players": [player_count]})
player_count_display<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
item_count=len(purchase_data["Item ID"].unique())
# Average Price
average_price=purchase_data["Price"].mean()
# Number of Purchases
number_purchases=len(purchase_data["Purchase ID"])
# Total Revenue
total_revenue=purchase_data["Price"].sum()

# Summary Data Frame
summary_table = pd.DataFrame({"Number of Unique Items": [item_count],
                            "Average Price": [average_price],
                            "Number of Purchases": [number_purchases],
                            "Total Revenue": [total_revenue]})

# Display numbers in cleaner format
#pd.options.display.float_format = '${:,.2f}'.format
summary_table ["Average Price"] = summary_table ["Average Price"].map("${:,.2f}".format)
summary_table ["Total Revenue"] =summary_table ["Total Revenue"].map("${:,.2f}".format)

# Display Data Frame
summary_table<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># Data Frame to remove duplicates by SN Column
#clean_data=pd.DataFrame.drop_duplicates()
#clean_data=purchase_data.groupby(["SN","Gender"]).count()
#clean_data_male=clean_data.loc[clean_data["Gender"]=="Male"]
#clean_data

#clean_data_new=clean_data[["SN", "Gender"]]
#clean_data_new

# Finding the Counts by Gender
player_count=len(purchase_data["SN"].unique())
male_player_count=len(purchase_data[purchase_data["Gender"]=="Male"]["SN"].unique())
female_player_count=len(purchase_data[purchase_data["Gender"]=="Female"]["SN"].unique())
remaining_count=player_count-(male_player_count + female_player_count)

# Finding the percentages
male_player_percent=round(((male_player_count / player_count) * 100),2)
female_player_percent=round(((female_player_count / player_count) * 100),2)
remaining_percent= round(100 - (male_player_percent + female_player_percent),2)

# Creating DataFrame to display results
gender_table = pd.DataFrame([
    {"":"Male","Total Count":male_player_count,"Percentage of Players":male_player_percent},
    {"":"Female","Total Count":female_player_count,"Percentage of Players":female_player_percent},
    {"":"Other/Non-Disclosed","Total Count":remaining_count,"Percentage of Players":remaining_percent}])

# Setting Gender as the Index
gender_table_display = gender_table.set_index("")

# Display Table
columnsTitles = ["Total Count","Percentage of Players"]
gender_table_display = gender_table_display.reindex(columns=columnsTitles)
gender_table_display<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># Total purchases
total_purchases=purchase_data["Purchase ID"].count()

# Purchase Count by Gender
male_purchases=purchase_data[purchase_data["Gender"]=="Male"]["Purchase ID"].count()

female_purchases=purchase_data[purchase_data["Gender"]=="Female"]["Purchase ID"].count()

remaining_purchases=total_purchases-(male_purchases + female_purchases)

# Average Price by Gender
male_price=purchase_data[purchase_data["Gender"]=="Male"]["Price"].mean()
female_price=purchase_data[purchase_data["Gender"]=="Female"]["Price"].mean()
remaining_price=purchase_data[purchase_data["Gender"]=="Other / Non-Disclosed"]["Price"].mean()

# Total Purchase Value
male_purchases_value=male_purchases * male_price
female_purchases_value=female_purchases * female_price
remaining_purchases_value=remaining_purchases * remaining_price

# Average Total Purchase Per Person
male_purchase_average = male_purchases_value / male_player_count
female_purchase_average = female_purchases_value / female_player_count
remaining_purchase_average = remaining_purchases_value / remaining_count

# Creating DataFrame to display results
purchasing_analysis_table = pd.DataFrame([
    {"Gender":"Female","Purchase Count":female_purchases,"Average Purchase Price":female_price,
     "Total Purchase Value":female_purchases_value,"Avg Total Purchase Per Person":female_purchase_average},
    
    {"Gender":"Male","Purchase Count":male_purchases,"Average Purchase Price":male_price,
     "Total Purchase Value":male_purchases_value,"Avg Total Purchase Per Person":male_purchase_average},
    
    {"Gender":"Other/Non-Disclosed","Purchase Count":remaining_purchases,"Average Purchase Price":remaining_price,
     "Total Purchase Value":remaining_purchases_value,"Avg Total Purchase Per Person":remaining_purchase_average}])

# Setting Gender as the Index
purchasing_analysis_display = purchasing_analysis_table.set_index("Gender")

# Display numbers in cleaner format
#pd.options.display.float_format = '${:,.2f}'.format
purchasing_analysis_display["Average Purchase Price"]=purchasing_analysis_display["Average Purchase Price"].map("${:,.2f}".format)
purchasing_analysis_display["Total Purchase Value"]=purchasing_analysis_display["Total Purchase Value"].map("${:,.2f}".format)
purchasing_analysis_display["Avg Total Purchase Per Person"]=purchasing_analysis_display["Avg Total Purchase Per Person"].map("${:,.2f}".format)

# Display Table
columnsTitles = ["Purchase Count","Average Purchase Price","Total Purchase Value","Avg Total Purchase Per Person"]
purchasing_analysis_display = purchasing_analysis_display.reindex(columns=columnsTitles)
purchasing_analysis_display<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>#Figure out min and max age
#print(purchase_data["Age"].max())
#print(purchase_data["Age"].min())

# Create Bins
bins = [0,9.9,14.9,19.9,24.9,29.9,34.9,39.9,50]

#Create group labels for these bins
group_labels = ["<10", "10-14","15-19","20-24","25-29","30-34","35-39","40+"]

#Create a new dataframe, keeping only SN, Gender and Age columns
purchase_data_new = purchase_data[["SN","Gender","Age"]]
#purchase_data_new

#Drop Duplicates from new Data Frame
purchase_data_new = purchase_data_new.drop_duplicates()
#purchase_data_new.describe()

#Place the data series into a new column inside of the DataFrame
purchase_data_new["Age Groups"] = pd.cut(purchase_data_new["Age"], bins, labels=group_labels)
#purchase_data_new.head()

#Exporting to a CSV File to test that bin grouping is working
#purchase_data_new.to_csv("fileOld.csv", index=False, header=True)

#Calculate the numbers and percentages by age group
age_count=purchase_data_new["Age Groups"].value_counts()
age_percent=round(age_count / player_count * 100,2)


#Create a summary data frame to hold the results
age_demographics = pd.DataFrame({"Total Count": age_count,"Percentage of Players": age_percent})

#Display the table and sort Index from lower to higher Bin
age_demographics
age_demographics.sort_index()<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>#Place the data series into a new column inside of the DataFrame
purchase_data["Age Groups"] = pd.cut(purchase_data["Age"], bins, labels=group_labels)

# Create a GroupBy object based upon "Age Groups"
purchase_group = purchase_data.groupby("Age Groups")

# Calculations
purchase_count = purchase_group["Purchase ID"].count()

avg_purchase_price = purchase_group["Price"].mean()

total_purchase_age = purchase_group["Price"].sum()

avg_total_value_person = total_purchase_age / (age_demographics["Total Count"])

#Create a summary data frame to hold the results
purchasing_analysis_age = pd.DataFrame({"Purchase Count": purchase_count,"Average Purchase Price": avg_purchase_price,
                                        "Total Purchase Value":total_purchase_age,
                                        "Avg Total Purchase Per Person":avg_total_value_person})

# Display numbers in cleaner format
#pd.options.display.float_format = '${:,.2f}'.format
purchasing_analysis_age["Average Purchase Price"]=purchasing_analysis_age["Average Purchase Price"].map("${:,.2f}".format)
purchasing_analysis_age["Total Purchase Value"]=purchasing_analysis_age["Total Purchase Value"].map("${:,.2f}".format)
purchasing_analysis_age["Avg Total Purchase Per Person"]=purchasing_analysis_age["Avg Total Purchase Per Person"].map("${:,.2f}".format)

#Display the table and sort Index from lower to higher Bin
purchasing_analysis_age
purchasing_analysis_age.sort_index()<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>#Calculations
total_purchase_count_SN = purchase_data.groupby("SN").count()["Price"]
avg_purchase_price_SN = purchase_data.groupby("SN").mean()["Price"]
total_purchase_value_SN = purchase_data.groupby("SN").sum()["Price"]

#Create a summary data frame to hold the results
total_spenders = pd.DataFrame({"Purchase Count": total_purchase_count_SN,"Average Purchase Price": avg_purchase_price_SN,
                                        "Total Purchase Value":total_purchase_value_SN})

# Display numbers in cleaner format
pd.options.display.float_format = '${:,.2f}'.format
# total_spenders["Average Purchase Price"]=total_spenders["Average Purchase Price"].map("${:,.2f}".format)
# total_spenders["Total Purchase Value"]=total_spenders["Total Purchase Value"].map("${:,.2f}".format)

#Display a summary of the dataframe by chooding the first 5 Names sorted from Highest to lowerst by Purchase Value
total_spenders.sort_values("Total Purchase Value", ascending=False).head(5)<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>#Retrieve the Item ID, Item Name, and Item Price columns by creating a new dataframe
popular_items=purchase_data[["Item ID","Item Name","Price"]]

#Group by Item ID and Item Name.Perform calculations to obtain purchase count, item price, and total purchase value
item_purchase_count = popular_items.groupby(["Item ID","Item Name"]).count()["Price"]
item_purchase_price = popular_items.groupby(["Item ID","Item Name"]).mean()["Price"]
item_purchase_value = popular_items.groupby(["Item ID","Item Name"]).sum()["Price"]

#Summary data frame to hold the results
items_summary = pd.DataFrame({"Purchase Count": item_purchase_count,"Item Price": item_purchase_price,
                                        "Total Purchase Value":item_purchase_value})

# Display numbers in cleaner format
pd.options.display.float_format = '${:,.2f}'.format
# items_summary["Item Price"]=items_summary["Item Price"].map("${:,.2f}".format)
# items_summary["Total Purchase Value"]=items_summary["Total Purchase Value"].map("${:,.2f}".format)

#Display the table, sorting by Purchase Count, and listing the top 5 items
items_summary.sort_values("Purchase Count", ascending=False).head(5)<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 above table by total purchase value in descending order
items_summary.sort_values("Total Purchase Value", ascending=False).head(5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1095383275261324, 'fraction_numerical': 0.008638211382113821, 'mean_word_length': 6.85014245014245, 'pattern_counts': {'":': 41, '<': 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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '307253', 'license': 'MIT', 'n_tokens_mistral': 4245, 'n_tokens_neox': 3972, 'n_words': 1123, 'path': 'HeroesOfPymoli_starter.ipynb', 'repo_name': 'DesthyKardell/Heroes-of-Pymoli-Data-Analysis', 'size': '13776'}