<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

# 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)

# added bins from later cells
bins = [0,9,14,19,24,29,34,39,50]
group_names = ['<10','10-14','15-19','20-24','25-29','30-34', '35-39', '40+']
purchase_data['Age Range'] = pd.cut(purchase_data['Age'], bins, labels=group_names)<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code># find unique number of SN and put in dataframe
total_players = purchase_data['SN'].nunique()
total_players_df = pd.DataFrame([{'Total Players': total_players}])

total_players_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># variables for df
uniqueItems = purchase_data['Item ID'].nunique()
avgPrice = purchase_data['Price'].mean()
purchases = purchase_data['Purchase ID'].count()
total = purchase_data['Price'].sum()

# setup df
overview_df = pd.DataFrame([{'Number of Unique Items': uniqueItems,'Average Price':avgPrice,'Number of Purchases':purchases,'Total Revenue':total}])

# format columns
overview_df["Average Price"] = overview_df["Average Price"].map("${:.2f}".format)
overview_df["Total Revenue"] = overview_df["Total Revenue"].map("${:.2f}".format)

overview_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># variables for df
unique_df = purchase_data.drop_duplicates(subset='SN',keep='first')

gender_df = unique_df.groupby(['Gender'])

count = gender_df.count()['SN']
total = int(len(unique_df.index))
percent = count / total

demo_df = pd.DataFrame({'Total Count':count,'Percentage of Players':percent})


# # Format percent
demo_df['Percentage of Players'] = demo_df['Percentage of Players'].map("{:.2%}".format)


demo_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>ana_df = purchase_data.groupby(['Gender'])

ana_count = ana_df['Price'].count()
ana_avg = ana_df['Price'].mean()
ana_sum =  ana_df['Price'].sum()
ana_pp = ana_sum / count

analysis_df = pd.DataFrame({'Purchase Count':ana_count,
                            'Average Purchase Price':ana_avg, 
                            'Total Purchase Value':ana_sum,
                            'Avg Total Purchase per Person':ana_pp})

# # Format percent
analysis_df['Average Purchase Price'] = analysis_df['Average Purchase Price'].map("${:.2f}".format)
analysis_df['Total Purchase Value'] = analysis_df['Total Purchase Value'].map("${:.2f}".format)
analysis_df['Avg Total Purchase per Person'] = analysis_df['Avg Total Purchase per Person'].map("${:.2f}".format)
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>age_df = unique_df.groupby('Age Range')


age_count = age_df['Age'].count()
age_percent = age_df['Age'].count() / total

age_demo_df =  pd.DataFrame({'Total Count':age_count,
                            'Percentage of Players':age_percent})

# # Format percent
age_demo_df['Percentage of Players'] = age_demo_df['Percentage of Players'].map("{:.2%}".format)

age_demo_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>purch_df = purchase_data.groupby('Age Range')

purch_total = int(len(purchase_data.index))
sum_total = purchase_data['Price'].sum()

purch_count = purch_df['Age'].count()
purch_sum = purch_df['Price'].sum()
purch_avg = purch_sum/purch_count
purch_pp = purch_sum/age_count





purchase_df = pd.DataFrame({'Purchase Count': purch_count, 
              'Average Purchase Price': purch_avg, 
              'Total Purchase Value': purch_sum, 
              'Avg Total Purchase per Person':purch_pp})

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


                          
purchase_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_df = purchase_data.groupby('SN', sort=False)  
top_count = top_df['SN'].count()
top_sum = top_df['Price'].sum()
top_avg = top_sum / top_count




# Fot Total Purchase Value
# .sort_values(ascending=False)




top_spend_df =  pd.DataFrame({'Purchase Count':top_count,
                              'Average Purchase Price':top_avg,
                              'Total Purchase Value':top_sum
}).sort_values('Total Purchase Value',ascending=False)

top_spend_df['Average Purchase Price'] = top_spend_df['Average Purchase Price'].map("${:.2f}".format)
top_spend_df['Total Purchase Value'] = top_spend_df['Total Purchase Value'].map("${:.2f}".format)


top_spend_df.head()<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>pop_df = purchase_data.groupby(['Item ID','Item Name'], sort=False)  

pop_count = pop_df['SN'].count()
pop_sum = pop_df['Price'].sum()
pop_price = pop_sum / pop_count

most_popular_df = pd.DataFrame({'Purchase Count':pop_count,
                              'Item Price':pop_price,
                              'Total Purchase Value':pop_sum}).sort_values(['Purchase Count','Item ID'],ascending=False)
most_popular_df['Item Price'] = most_popular_df['Item Price'].map("${:.2f}".format)
most_popular_df['Total Purchase Value'] = most_popular_df['Total Purchase Value'].map("${:.2f}".format)

most_popular_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>profit_df = purchase_data.groupby(['Item ID','Item Name'], sort=False)  

profit_count = profit_df['SN'].count()
profit_sum = profit_df['Price'].sum()
profit_price = profit_sum / profit_count

most_profitable_df = pd.DataFrame({'Purchase Count':pop_count,
                              'Item Price':pop_price,
                              'Total Purchase Value':pop_sum}).sort_values(['Total Purchase Value','Item ID'],ascending=False)
most_profitable_df['Item Price'] = most_profitable_df['Item Price'].map("${:.2f}".format)
most_profitable_df['Total Purchase Value'] = most_profitable_df['Total Purchase Value'].map("${:.2f}".format)

most_profitable_df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12056818181818182, 'fraction_numerical': 0.010227272727272727, 'mean_word_length': 5.197887323943662, 'pattern_counts': {'":': 0, '<': 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': '616794', 'license': 'ADSL', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2583, 'n_words': 758, 'path': 'HeroesOfPymoli/HeroesOfPymoli_starter.ipynb', 'repo_name': 'HotAsPho/pandas-challenge', 'size': '8800'}