<jupyter_start><jupyter_text>Player CountTotal Number of Players<jupyter_code>total_players = len(purchase_data["SN"].unique())
total_players

total_players_count = pd.DataFrame({"Total Players":[total_players]},columns=["Total Players"])
total_players_count<jupyter_output><empty_output><jupyter_text>Purchasing Analysis (Total)Number of Unique Items,Average Purchase Price,Total Number of Purchases,Total Revenue,<jupyter_code>number_of_unique_item = len(purchase_data["Item ID"].unique())
average_price = round(purchase_data["Price"].mean(),2)
number_purchase = purchase_data["Price"].count()
total_revenue = purchase_data["Price"].sum()

summary_table = pd.DataFrame({
    "Number of Unique Items": [number_of_unique_item],
    "Average Price":[average_price ],
    "Number of Purchase" :[number_purchase],
    "Total Revenue":[total_revenue]
})

summary_table["Average Price"]= summary_table["Average Price"].map("${}".format)
summary_table["Total Revenue"]= summary_table["Total Revenue"].map("${}".format)
summary_table.head()<jupyter_output><empty_output><jupyter_text>Gender DemographicsPercentage and Count of Male Players,Percentage and Count of Female Players,Percentage and Count of Other / Non-Disclosed<jupyter_code>gender_demo = purchase_data.groupby("Gender")


#refrence Series.nunique(dropna=True)[source]
#https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.nunique.html
summary_gender = gender_demo.nunique()["SN"]
percent_players = summary_gender / total_players * 100

gender_pd = pd.DataFrame ({"Total Count":summary_gender,
                "Percentage of Players": percent_players
                          })
gender_pd.index.name = None

gender_pd["Percentage of Players"]= gender_pd["Percentage of Players"].map("{:.2f}%".format)
gender_pd.head()<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 genderCreate a summary data frame to hold the resultsOptional: give the displayed data cleaner formattingDisplay the summary data frame<jupyter_code>pur_count = gender_demo["Purchase ID"].count()

avg_pur_price = gender_demo ["Price"].mean()

total_pur_value = gender_demo ["Price"].sum()

avg_pur_total_pperson = total_pur_value/summary_gender






gen_pur_df = pd.DataFrame({"Purchase Count":pur_count,
                           "Average Purchase Price":avg_pur_price,
                           "Total Purchase Value":total_pur_value,
                           "Avg Total Purchase per Person":avg_pur_total_pperson
                         })
gen_pur_df["Average Purchase Price"]= gen_pur_df["Average Purchase Price"].map("${:.2f}".format)
gen_pur_df["Total Purchase Value"]= gen_pur_df["Total Purchase Value"].map("${:.2f}".format)                   
gen_pur_df["Avg Total Purchase per Person"] = gen_pur_df["Avg Total Purchase per Person"].map("${:.2f}".format)

gen_pur_df.head()<jupyter_output><empty_output><jupyter_text>Age Demographics Establish bins for agesCategorize the existing players using the age bins. Hint: use pd.cut()Calculate the numbers and percentages by age groupCreate a summary data frame to hold the resultsOptional: round the percentage column to two decimal pointsDisplay Age Demographics Table<jupyter_code>bins = [0, 9, 14, 19, 24, 29, 35, 40, 99]
group_lables=["<10", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40+"]

purchase_data["Age"] = pd.cut(purchase_data["Age"],bins, labels=group_lables)

age_grp = purchase_data.groupby("Age")

total_age_count = age_grp["SN"].nunique()

percentage_age = (total_age_count/total_players) * 100

age_dem_pd = pd.DataFrame({"Total Count": total_age_count,
                          "Percentage of Players":percentage_age})
age_dem_pd.index.name = None



age_dem_pd["Percentage of Players"]= age_dem_pd["Percentage of Players"].map("{:.2f}".format)
age_dem_pd<jupyter_output><empty_output><jupyter_text>Purchasing Analysis (Age) Bin the purchase_data data frame by ageRun basic calculations to obtain purchase count, avg. purchase price, avg. purchase total per person etc. in the table belowCreate a summary data frame to hold the resultsOptional: give the displayed data cleaner formattingDisplay the summary data frame<jupyter_code>pur_age_count = age_grp["Purchase ID"].count()
age_avg_pur = age_grp["Price"].mean()

age_total_value = age_grp["Price"].sum()

avg_pur_total_per_person = age_total_value/total_age_count

purchasing_analysis_pd = pd.DataFrame({"Purchase Count": pur_age_count,
                                       "Average Purchase Price": age_avg_pur,
                                       "Total Purchase Value": age_total_value,
                                       "Avg Total Purchase per Person":avg_pur_total_per_person
                                       })

purchasing_analysis_pd




# purchasing_analysis_pd["Avergae Purchase Price"]= purchasing_analysis_pd["Average Purchase Price"].map("${:.2f}".format)
purchasing_analysis_pd["Total Purchase Value"]= purchasing_analysis_pd["Total Purchase Value"].map("${:.2f}".format)
purchasing_analysis_pd["Avg Total Purchase per Person"]= purchasing_analysis_pd["Avg Total Purchase per Person"].map("${:.2f}".format)

purchasing_analysis_pd<jupyter_output><empty_output><jupyter_text>Top Spenders Run basic calculations to obtain the results in the table belowCreate a summary data frame to hold the resultsSort the total purchase value column in descending orderOptional: give the displayed data cleaner formattingDisplay a preview of the summary data frame<jupyter_code>spenders = purchase_data.groupby ("SN")

spenders_pur_count = spenders["Purchase ID"].count()

spenders_avg_pur_price = spenders ["Price"].mean()

spenders_total_pur_val = spenders["Price"].sum()

top_spenders_pd = pd.DataFrame({"Purchase Count":spenders_pur_count,
                               "Average Purchase Price":spenders_avg_pur_price,
                               "Total Purchase Value": spenders_total_pur_val
                               })

format_spenders = top_spenders_pd.sort_values(["Total Purchase Value"], ascending=False).head()
format_spenders["Average Purchase Price"]= format_spenders["Average Purchase Price"].map("${:.2f}".format)
format_spenders["Total Purchase Value"]= format_spenders["Total Purchase Value"].map("${:.2f}".format)

format_spenders.head()<jupyter_output><empty_output><jupyter_text>Most Popular Items Retrieve the Item ID, Item Name, and Item Price columnsGroup by Item ID and Item Name. Perform calculations to obtain purchase count, item price, and total purchase valueCreate a summary data frame to hold the resultsSort the purchase count column in descending orderOptional: give the displayed data cleaner formattingDisplay a preview of the summary data frame<jupyter_code>pop_items = purchase_data [["Item ID","Item Name", "Price"]]

grp_items = pop_items.groupby(["Item ID", "Item Name"])

item_pur_count = grp_items ["Price"].count()

item_pur_val = (grp_items["Price"].sum())

item_price = item_pur_val/item_pur_count

most_pop_item_pd = pd.DataFrame({"Purchase Count": item_pur_count,
                                "Item Price":item_price,
                                "Total Purchase Value": item_pur_val
                                })
#most_pop_item_pd.head()
pop_item_format = most_pop_item_pd.sort_values(["Purchase Count"], ascending=False).head()
pop_item_format["Item Price"]= pop_item_format["Item Price"].map("${:.2f}".format)
pop_item_format["Total Purchase Value"]= pop_item_format["Total Purchase Value"].map("${:.2f}".format)

pop_item_format<jupyter_output><empty_output><jupyter_text>Most Profitable Items Sort the above table by total purchase value in descending orderOptional: give the displayed data cleaner formattingDisplay a preview of the data frame<jupyter_code>pop_item_format = most_pop_item_pd.sort_values(["Total Purchase Value"], ascending=False).head()
pop_item_format["Item Price"]= pop_item_format["Item Price"].map("${:.2f}".format)
pop_item_format["Total Purchase Value"]= pop_item_format["Total Purchase Value"].map("${:.2f}".format)

pop_item_format<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1134033203125, 'fraction_numerical': 0.0079345703125, 'mean_word_length': 4.997803806734993, 'pattern_counts': {'":': 22, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '361382', 'license': 'MIT', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2416, 'n_words': 582, 'path': 'Heroes/Pandas Homework.ipynb', 'repo_name': 'Melow49/Pandas-Homework', 'size': '8192'}