<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)<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code>#Creates a new data from purchases_data that shows only total number of players
Player_Count=pd.DataFrame({"Total Players": [purchase_data["SN"].nunique()]})
Player_Count<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 = purchase_data["Item ID"].copy().nunique()
Average_Price = purchase_data["Price"].copy().mean()
Num_Purchases = purchase_data["Purchase ID"].copy().count()
Total_Rev = purchase_data["Price"].copy().sum()
Purchase_Analysis_Total = pd.DataFrame({"Number of Unique Items":[Unique_Items],"Average Price":[Average_Price],"Number of Purchases":[Num_Purchases],"Total Revenue":[Total_Rev]})
Purchase_Analysis_Total["Average Price"] = Purchase_Analysis_Total["Average Price"].astype(float).map("${:,.2f}".format)
Purchase_Analysis_Total["Total Revenue"] = Purchase_Analysis_Total["Total Revenue"].astype(float).map("${:,.2f}".format)
Purchase_Analysis_Total<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>Gender_Demographics = purchase_data.groupby(["Gender"]).nunique()
Gender_Count = Gender_Demographics["SN"]
Gender_Percentage = (Gender_Demographics.loc[:,"SN"].astype(int)/Player_Count.loc[0,"Total Players"].astype(int))*100
Gender_Demographics["Percentage of Players"] = Gender_Percentage
Gender_Demographics = Gender_Demographics.loc[:,["SN"]]
Gender_Demographics["Percentage of Players"]=Gender_Percentage
Gender_Demographics.rename(columns={"SN": "Total Count"}, inplace = True)
Gender_Demographics["Percentage of Players"] = Gender_Demographics["Percentage of Players"].astype(float).map("{:.2f}%".format)
Gender_Demographics<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>#Purchases_By_Gender = purchase_data.groupby(["Gender"]).nunique()
purchase_by_gender = purchase_data.groupby("Gender")
purchase_count = purchase_by_gender["Purchase ID"].nunique().astype(int)
avg_purchase_price = purchase_by_gender["Price"].mean().astype(float).map("${:,.2f}".format)
total_purchase_price = purchase_by_gender["Price"].sum().astype(float)
avg_total_purchase_per_person=(total_purchase_price/Gender_Demographics["Total Count"]).map("${:,.2f}".format)
purchasing_analysis_by_gender_df=pd.DataFrame({"Purchase Count": purchase_count,"Average Purchase Price":avg_purchase_price,"Total Purchase Value":total_purchase_price.map("${:,.2f}".format),"Avg Total Purchase per Person": avg_total_purchase_per_person})
purchasing_analysis_by_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>#bins for ages
bins=[0,9.9,14.9,19.9,24.9,29.9,34.9,39.9,100]
ages=["<10","10-14","15-19","20-24","25-29","30-34","35-39","40+"]
purchase_data["Age Ranges"]=pd.cut(purchase_data["Age"],bins,labels=ages,include_lowest=True)
age_demographics=purchase_data.groupby("Age Ranges")
total_count_age=age_demographics.nunique()["SN"].astype(int)
percentage_of_players_age=(((total_count_age/total_count_age.sum()))*100).astype(float).map("{:,.2f}%".format)
age_demographics_df=pd.DataFrame({"Total Count":total_count_age,"Percentage of Players":percentage_of_players_age})
age_demographics_df<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>purchase_analysis_age = purchase_data.groupby("Age Ranges")
#Purchase_Analysis_Age = Purchase_Analysis_Age.nunique()
purchase_count_by_age = purchase_analysis_age.nunique()["Purchase ID"].astype(int)
total_purchase_value_by_age=purchase_analysis_age["Price"].sum().astype(float)
avg_purchase_price_by_age = (total_purchase_value_by_age/purchase_count_by_age).map("${:,.2f}".format)
avg_total_purchase_by_person= (total_purchase_value_by_age/total_count_age).map("${:,.2f}".format)
Purchase_Analysis_Age_df= pd.DataFrame({"Purchase Count":purchase_count_by_age, "Average Purchase Price": avg_purchase_price_by_age,"Total Purchase Value": total_purchase_value_by_age.map("${:,.2f}".format), "Avg Total Purchase per Person": avg_total_purchase_by_person})
Purchase_Analysis_Age_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>top_spenders=purchase_data.groupby("SN")
top_total_purchase_value = top_spenders["Price"].sum().astype(float)
top_purchase_count = top_spenders['Purchase ID'].count().astype(int)
top_average_purchase_price = (top_total_purchase_value/top_purchase_count).map("${:,.2f}".format)
top_spenders_df = pd.DataFrame({"Purchase Count":top_purchase_count,"Average Purchase Price":top_average_purchase_price,"Total Purchase Value":top_total_purchase_value.map("${:,.2f}".format)})
top_spenders_df = top_spenders_df.sort_values(["Purchase Count", "Total Purchase Value"], ascending=False)
top_spenders_df.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, 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>most_pop = purchase_data.groupby(["Item ID","Item Name"]).agg({"Price": ['count','sum']})
most_pop.columns=["Purchase Count","Total Purchase Value"]
most_pop.insert(1,"Item Price", (most_pop["Total Purchase Value"]/most_pop["Purchase Count"]).astype(float).map("${:,.2f}".format), True)

most_pop_df=most_pop.reset_index()
most_pop_df["Total Purchase Value"]=most_pop_df["Total Purchase Value"].map("${:,.2f}".format)
#total_purchase_value_pop=most_pop.loc[:,"Total Purchase Price"].map("${:,.2f}".format)
#del most_pop['Total Purchase Price']
#most_pop=most_pop.add(total_purchase_value_pop)
most_pop_df=most_pop_df.set_index("Item ID")
most_pop_df=most_pop_df.sort_values(["Purchase Count", "Total Purchase Value"], ascending=False)
most_pop_df.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>most_prof_sort=most_pop.sort_values(["Total Purchase Value"], ascending=False)
most_prof_sort["Total Purchase Value"]=most_prof_sort["Total Purchase Value"].map("${:,.2f}".format)
most_prof_sort=most_prof_sort.reset_index()
most_prof_sort=most_prof_sort.set_index("Item ID")
most_prof_sort.head(5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12246351014595942, 'fraction_numerical': 0.009374629168149995, 'mean_word_length': 9.548185231539424, 'pattern_counts': {'":': 20, '<': 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': '632687', 'license': 'ADSL', 'n_tokens_mistral': 2766, 'n_tokens_neox': 2605, 'n_words': 599, 'path': 'HeroesOfPymoli/HeroesOfPymoli_starter.ipynb', 'repo_name': 'georgiafbi/pandas-challenge', 'size': '8427'}