<jupyter_start><jupyter_text>Player Count Display the total number of players<jupyter_code># define player count and display in dataframe
player_count = len(purchase_df["SN"].value_counts())
player_count_df = pd.DataFrame(
    {"Total Players": [player_count]
    })

player_count_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 resultsOptional: give the displayed data cleaner formattingDisplay the summary data frame<jupyter_code># run calculations within dataframe. create summary dataframe to hold results
analysis_df = pd.DataFrame(
    {"Number of Unique Items": [len(purchase_df["Item Name"].value_counts())],
     "Average Purchase Price": purchase_df["Price"].mean(),
     "Total Number of Purchases": [len(purchase_df["Purchase ID"].value_counts())],
     "Total Revenue": purchase_df["Price"].sum()
    })

# give the displayed dataframe cleaner formatting
analysis_df = analysis_df.style.format({"Average Purchase Price": '${:,.2f}',
                                        "Total Revenue": '${:,.2f}'
                                       })

analysis_df<jupyter_output><empty_output><jupyter_text>Gender Demographics Percentage and Count of Male PlayersPercentage and Count of Female PlayersPercentage and Count of Other / Non-Disclosed<jupyter_code># create groupby for Gender by unique instances of SN
gender_count = purchase_df.groupby(["Gender"])["SN"].nunique()

# calculate gender percentage
gender_percent = gender_count / player_count * 100

# create summary dataframe to hold results. format data within dataframe
gender_demographics = pd.DataFrame({"Total Count": gender_count,
                                    "Percentage of Players": gender_percent.map("{:.2f}%".format)
                                   })

# sort function for highest to lowest Total Count
gender_demographics.sort_values('Total Count', ascending = False)<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># create groupby for Gender
gender_grouped = purchase_df.groupby(["Gender"])

# run calculations
purchase_count = gender_grouped["Purchase ID"].count()

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

total_purchase_value = gender_grouped["Price"].sum()

avg_total_by_gender = total_purchase_value / gender_count

# create summary dataframe to hold results. format data within dataframe
purchasing_analysis_df = pd.DataFrame({"Purchase Count": purchase_count,
                                       "Average Purchase Price": avg_purchase_price.map("${:.2f}".format),
                                       "Total Purchase Value": total_purchase_value.map("${:.2f}".format),
                                       "Avg Total Purchase per Person": avg_total_by_gender.map("${:.2f}".format)
                                      })
purchasing_analysis_df<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># establish bins for Age Groups
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+"]

# categorize the data according to the Age Group bin
purchase_df["Age Groups"] = pd.cut(purchase_df["Age"], bins, labels = group_names, include_lowest = True)

# run calculations
age_by_players = purchase_df.groupby(["Age Groups"])["SN"].nunique()

age_grouped = purchase_df.groupby(["Age Groups"])

percentage_of_players = age_by_players / player_count * 100

# create summary dataframe to hold results. format data within dataframe
age_groups = pd.DataFrame({"Total Count": age_by_players,
                           "Percentage of Players": percentage_of_players.map("{:.2f}%".format)
                          })
age_groups<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># run calculations
age_purchase_count = age_grouped["Purchase ID"].count()

age_avg_purchase_price = age_grouped["Price"].mean()

age_total_purchase_value = age_grouped["Price"].sum()

avg_total_by_age = age_total_purchase_value / age_purchase_count

# create summary dataframe to hold results. format data within dataframe
pa_age_df = pd.DataFrame({"Purchase Count": age_purchase_count,
                          "Average Purchase Price": age_avg_purchase_price.map("${:.2f}".format),
                          "Total Purchase Value": age_total_purchase_value.map("${:.2f}".format),
                          "Avg Total Purchase per Person": avg_total_by_age.map("${:.2f}".format)
                         })
pa_age_df<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># create groupby for SN
top_spenders = purchase_df.groupby(["SN"])

# run calculations
ts_purchase_count = top_spenders["Purchase ID"].count()

ts_avg_purchase_price = top_spenders["Price"].mean()

ts_total_purchase = top_spenders["Price"].sum()

# create summary dataframe to hold results. format data within dataframe
spenders_df = pd.DataFrame({"Purchase Count": ts_purchase_count,
                            "Average Purchase Price": ts_avg_purchase_price.map("${:.2f}".format),
                            "Total Purchase Value": ts_total_purchase.map("${:.2f}".format)
                           })

# sort function for highest to lowest Purchase Count
top_spenders_df = spenders_df.sort_values("Purchase Count", ascending = False)

top_spenders_df.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, average 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 formatting    Display a preview of the summary data frame<jupyter_code># create groupby for Item ID and Item Name
items_grouped = purchase_df.groupby(["Item ID","Item Name"])

# run calculations
pi_purchase_count = items_grouped["Purchase ID"].count()

item_price = items_grouped["Price"].mean()

pi_total_purchase_value = items_grouped["Price"].sum()

# create summary dataframe to hold results. format data within dataframe
items_df = pd.DataFrame({"Purchase Count": pi_purchase_count,
                         "Item Price": item_price.map("${:.2f}".format),
                         "Total Purchase Value": pi_total_purchase_value.map("${:.2f}".format),
                        })

# sort function for highest to lowest Purchase Count
popular_items_df = items_df.sort_values("Purchase Count", ascending = False)

popular_items_df.head(5)<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># create summary dataframe to hold results. format data within dataframe
items2_df = pd.DataFrame({"Purchase Count": pi_purchase_count,
                          "Item Price": item_price,
                          "Total Purchase Value": pi_total_purchase_value
                         })

# sort function for highest to lowest Total Purchase Value
most_profitable_df = items2_df.sort_values("Total Purchase Value", ascending = False)

# give the displayed dataframe cleaner formatting
most_profitable_df = most_profitable_df.head(10).style.format({"Item Price": '${:,.2f}',
                                                               "Total Purchase Value": '${:,.2f}'
                                                              })
most_profitable_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09241300627495722, 'fraction_numerical': 0.008214489446662863, 'mean_word_length': 3.7771117166212536, 'pattern_counts': {'":': 30, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '441592', 'license': 'ADSL', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2361, 'n_words': 741, 'path': 'heroes_of_pymoli/heroes_of_pymoli_main.ipynb', 'repo_name': 'vandrade88/pandas-challenge', 'size': '8765'}