<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_df = pd.read_csv(file_to_load)
purchase_data_df.head()<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code>#length to get total number of players
count_df = purchase_data_df["SN"].nunique()
#create Dataframe using dictionary of list
total_df = pd.DataFrame({"Total Players": [count_df]})
#set new index to Total Players
total_df = total_df.set_index("Total Players")
#display
total_df<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>#.nunique() function return number of unique elements in the object
items = purchase_data_df["Item Name"].nunique()
#.mean function return average
average_price = purchase_data_df["Price"].mean()
#number of purchases by counting the length of the dataframe
number_of_purchase = len(purchase_data_df)
#.sum function returns the total
total_revenue = purchase_data_df["Price"].sum()

#create Dataframe using lists of dictionaries
purchasing_analysis_df =pd.DataFrame([{"Number of Unique Items": items, "Average Price": average_price, "Number of Purchases": number_of_purchase, "Total Revenue": total_revenue}])

#use mapping to format dollar amounts
purchasing_analysis_df["Average Price"] = purchasing_analysis_df["Average Price"].map("${:.2f}".format)
purchasing_analysis_df["Total Revenue"] = purchasing_analysis_df["Total Revenue"].map("${:.2f}".format)

#set new index to Number of Unique Items
purchasing_analysis_df = purchasing_analysis_df.set_index("Number of Unique Items")

purchasing_analysis_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># Calculate the Number of Unique Players by gender and sn andded age for later in the code
demographic_df = purchase_data_df.loc[:, ["Gender", "SN", "Age" ]]
demographic_df = demographic_df.drop_duplicates()
gender_count = demographic_df.count()["Gender"]

# Display the total number of players by gender
gender_total = demographic_df["Gender"].value_counts()
gender_percent = gender_total / gender_count * 100

demographic_breakdown_df = pd.DataFrame({"Total Count": gender_total, "Percentage of Players": gender_percent})

demographic_breakdown_df["Percentage of Players"] = demographic_breakdown_df["Percentage of Players"].map("{0:.2f}%".format)

demographic_breakdown_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>#calculate the count average and sum by gender
gender_group = purchase_data_df.groupby(["Gender"])
gender_purchase_total = gender_group["SN"].count()
gender_average_price = purchase_data_df.groupby(["Gender"])["Price"].mean()
gender_total_price = purchase_data_df.groupby(["Gender"])["Price"].sum()
gender_per_person = gender_total_price / gender_total 

#create Dataframe
gender_purchasing_analysis_df =pd.DataFrame({"Purchase Count": gender_purchase_total, "Average Purchase Price": gender_average_price, "Total Purchase Value": gender_total_price, "Avg Total Purchase per Person": gender_per_person})

#use mapping to formatting
gender_purchasing_analysis_df["Average Purchase Price"] = gender_purchasing_analysis_df["Average Purchase Price"].map("${:.2f}".format)
gender_purchasing_analysis_df["Total Purchase Value"] = gender_purchasing_analysis_df["Total Purchase Value"].map("${:.2f}".format)
gender_purchasing_analysis_df["Avg Total Purchase per Person"] = gender_purchasing_analysis_df["Avg Total Purchase per Person"].map("${:.2f}".format)

#Display
gender_purchasing_analysis_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>#create bins
bins = [0, 9.90, 14.90, 19.90, 24.90, 29.90, 34.90, 39.90, 99999]
group_players = ["<10", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40+"]

#Categorize the existing players using the age bins
demographic_df[ "Age Groups"] = pd.cut(demographic_df["Age"], bins, labels = group_players)

#count age groups and calculate percentages
age_demographic_count = demographic_df["Age Groups"].value_counts()
age_demographic_percent = age_demographic_count / gender_count * 100

#use mapping to format
age_breakdown_df = pd.DataFrame({"Total Count": age_demographic_count,"Percentage of Player": age_demographic_percent})

#use mapping to format
age_breakdown_df["Percentage of Player"] = age_breakdown_df["Percentage of Player"].map("{0:.2f}%".format)

#sort by age group and display
age_breakdown_df.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>#Categorize the existing players using the age bins
purchase_data_df[ "Age Groups"] = pd.cut(purchase_data_df["Age"], bins, labels = group_players)

#calculate the count average and sum by age
gender_group = purchase_data_df.groupby(["Age Groups"])
age_purchase_total = gender_group["Age"].count()
age_average_price = purchase_data_df.groupby(["Age Groups"])["Price"].mean()
age_total_price = purchase_data_df.groupby(["Age Groups"])["Price"].sum()
age_per_person = age_total_price / age_demographic_count

#create Dataframe
age_purchasing_analysis_df =pd.DataFrame({"Purchase Count": age_purchase_total, "Average Purchase Price": age_average_price, "Total Purchase Value": age_total_price, "Avg Total Purchase per Person": age_per_person})

#use mapping to formatting
age_purchasing_analysis_df["Average Purchase Price"] = age_purchasing_analysis_df["Average Purchase Price"].map("${:.2f}".format)
age_purchasing_analysis_df["Total Purchase Value"] = age_purchasing_analysis_df["Total Purchase Value"].map("${:.2f}".format)
age_purchasing_analysis_df["Avg Total Purchase per Person"] = age_purchasing_analysis_df["Avg Total Purchase per Person"].map("${:.2f}".format)

#Display
age_purchasing_analysis_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>#calculate the count average and sum by gender
user_group = purchase_data_df.groupby(["SN"])
user_purchase_total = user_group["Price"].count()
user_average_price = purchase_data_df.groupby(["SN"])["Price"].mean()
user_total_price = purchase_data_df.groupby(["SN"])["Price"].sum()
user_per_person = gender_total_price / gender_total 

#create Dataframe
user_purchasing_analysis_df =pd.DataFrame({"Purchase Count": user_purchase_total, "Average Purchase Price": user_average_price, "Total Purchase Value": user_total_price})

#sort by purchase count then purchase value
sorted_user_purchasing_analysis_df = user_purchasing_analysis_df.sort_values("Total Purchase Value", ascending = False)

#use mapping to formatting
sorted_user_purchasing_analysis_df["Average Purchase Price"] = sorted_user_purchasing_analysis_df["Average Purchase Price"].map("${:.2f}".format)
sorted_user_purchasing_analysis_df["Total Purchase Value"] = sorted_user_purchasing_analysis_df["Total Purchase Value"].map("${:.2f}".format)

#Display first 10 rows
sorted_user_purchasing_analysis_df.head(10)<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, average 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>#calculate the count average and sum by item
item_group = purchase_data_df.groupby(["Item ID", "Item Name"])
item_purchase_total = item_group["Purchase ID"].count()
item_price = item_group["Price"].mean()
item_total_price = item_group["Price"].sum()

#create Dataframe
item_purchasing_analysis_df =pd.DataFrame({"Purchase Count": item_purchase_total, "Item Price": item_price, "Total Purchase Value": item_total_price})

#sort by purchase count then purchase count then total purchase value
sorted_item_purchasing_analysis_df = item_purchasing_analysis_df.sort_values(by=["Purchase Count","Total Purchase Value"], ascending = [False,False])

#use mapping to formatting
sorted_item_purchasing_analysis_df["Item Price"] = sorted_item_purchasing_analysis_df["Item Price"].map("${:.2f}".format)
sorted_item_purchasing_analysis_df["Total Purchase Value"] = sorted_item_purchasing_analysis_df["Total Purchase Value"].map("${:.2f}".format)

#Display header
sorted_item_purchasing_analysis_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>#calculate the count average and sum by item
item_group = purchase_data_df.groupby(["Item ID", "Item Name"])
item_purchase_total = item_group["Purchase ID"].count()
item_price = item_group["Price"].mean()
item_total_price = item_group["Price"].sum()

#create Dataframe
item_purchasing_analysis_df =pd.DataFrame({"Purchase Count": item_purchase_total, "Item Price": item_price, "Total Purchase Value": item_total_price})

#sort by total purchase value
sorted_item_purchasing_analysis_df = item_purchasing_analysis_df.sort_values("Total Purchase Value", ascending = False)

#use mapping to formatting
sorted_item_purchasing_analysis_df["Item Price"] = sorted_item_purchasing_analysis_df["Item Price"].map("${:.2f}".format)
sorted_item_purchasing_analysis_df["Total Purchase Value"] = sorted_item_purchasing_analysis_df["Total Purchase Value"].map("${:.2f}".format)

#Display header
sorted_item_purchasing_analysis_df.head()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup