<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)<jupyter_output><empty_output><jupyter_text>Player Count * Display the total number of players<jupyter_code># Create player_count variable to filter the unique number of players
player_count_df = purchase_data.drop_duplicates(subset=['SN'])
player_count = player_count_df.count()
# Create a summary data frame to show total number of players
total_player_df = pd.DataFrame({'Total Players': [player_count['SN']]})
total_player_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># Create variables separately to store the calculations
number_of_unique_items = purchase_data.drop_duplicates(subset=['Item Name']).count()
average_price = purchase_data['Price'].mean()
number_of_purchases = purchase_data['Item Name'].count()
total_revenue = purchase_data['Price'].sum()

# Create summary data frame to show the results
purchasing_analysis = pd.DataFrame({
    'Number of Unique Items': [number_of_unique_items['Item Name']],
    'Average Price': '${:,.2f}'.format(average_price),
    'Number of Purchases': [number_of_purchases],
    'Total Revenue': '${:,.2f}'.format(total_revenue)
})

purchasing_analysis<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># Use the 'player_count_df' data frame that was previously created to calculate total number of players (this data frame only includes the non-duplicated players) to arrange the data by Gender
gender_df = player_count_df.set_index('Gender')
player_count_df

# Create variables to store gender demographics calculations
male_count = gender_df.loc['Male','SN'].count()
female_count = gender_df.loc['Female','SN'].count()
other_count = gender_df.loc['Other / Non-Disclosed','SN'].count()

total_players = player_count['SN']     # This variable was used in the Total Number of Players calculations
male_pct = male_count / total_players
female_pct = female_count / total_players
other_pct = other_count / total_players

# Create summary data frame
data_gender = {
    'Total Count': [male_count, female_count, other_count],
    'Percentage of Players': [male_pct, female_pct, other_pct]
}
gender_demo = pd.DataFrame(data_gender, index=['Male',
                                        'Female',
                                        'Other / Non-Disclosed'])

# Format the percentages
gender_demo['Percentage of Players'] = gender_demo['Percentage of Players'].map('{:.2%}'.format)
gender_demo<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># Group the purchase data by gender
gender_purchase_df = purchase_data.set_index('Gender')

# Create variables to store the calculations for purchasing data
male_purchase_count = gender_purchase_df.loc['Male','Item Name'].count()
female_purchase_count = gender_purchase_df.loc['Female','Item Name'].count()
other_purchase_count = gender_purchase_df.loc['Other / Non-Disclosed','Item Name'].count()

male_purchase_total = gender_purchase_df.loc['Male','Price'].sum()
female_purchase_total = gender_purchase_df.loc['Female','Price'].sum()
other_purchase_total = gender_purchase_df.loc['Other / Non-Disclosed','Price'].sum()

male_purchase_avg = male_purchase_total / male_purchase_count
female_purchase_avg = female_purchase_total / female_purchase_count
other_purchase_avg = other_purchase_total / other_purchase_count

male_purchase = male_purchase_total / male_count
female_purchase = female_purchase_total / female_count
other_purchase = other_purchase_total / other_count

# Create summary data frame
data_purchase = {
    'Purchase Count': [male_purchase_count, female_purchase_count, other_purchase_count],
    'Average Purchase Price': [male_purchase_avg, female_purchase_avg, other_purchase_avg],
    'Total Purchase Value': [male_purchase_total, female_purchase_total, other_purchase_total],
    'Avg Total Purchase per Person': [male_purchase, female_purchase, other_purchase]
}
purchase_df = pd.DataFrame(data_purchase, index=['Male',
                                        'Female',
                                        'Other / Non-Disclosed'])
# Format the percentages
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>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 the bins in which Data will be held
age_to_cut = [0, 9.9, 14.9, 19.9, 24.9, 29.9, 34.9, 39.9, 100]
# Create the names for the bins
age_group_name = ['<10', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40+']

# Create age groups using the dataset with non-duplicate players
age_groups = pd.cut(
    x=player_count_df['Age'],
    bins=age_to_cut,
    labels=age_group_name,
    include_lowest=True,
)

age_count = player_count_df.groupby(age_groups)['SN'].count()
pct_count = age_count / total_players
    
# Create summary data frame
data_age = {
    'Total Count': age_count,
    'Percentage of Players': pct_count
}
age_demo = pd.DataFrame(data_age, index=age_group_name)

# Format the percentages
age_demo['Percentage of Players'] = age_demo['Percentage of Players'].map('{:.2%}'.format)
age_demo<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># Create age groups using the original dataset
age_groups = pd.cut(
    x=purchase_data['Age'],
    bins=age_to_cut,
    labels=age_group_name,
    include_lowest=True,
)

purchase_count = purchase_data.groupby(age_groups)['Item Name'].count()
purchase_total = purchase_data.groupby(age_groups)['Price'].sum()

purchase_avg = purchase_total / purchase_count
purchase_avg_person = purchase_total / age_count

    
# Create summary data frame
data_purchase = {
    'Purchase Count': purchase_count,
    'Average Purchase Price': purchase_avg,
    'Total Purchase Value': purchase_total,
    'Avg Total Purchase per Person': purchase_avg_person
}
purchase_demo = pd.DataFrame(data_purchase, index=age_group_name)

# Format the floats
purchase_demo['Average Purchase Price'] = purchase_demo['Average Purchase Price'].map('${:,.2f}'.format)
purchase_demo['Total Purchase Value'] = purchase_demo['Total Purchase Value'].map('${:,.2f}'.format)
purchase_demo['Avg Total Purchase per Person'] = purchase_demo['Avg Total Purchase per Person'].map('${:,.2f}'.format)
purchase_demo<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># Create spender_df data frame to make 'SN' as index
spender_df = purchase_data.set_index('SN')

# Add the analysis columns to the data frame
spender_count = spender_df.groupby(['SN'])['Item Name'].count()
spender_total = spender_df.groupby(['SN'])['Price'].sum()
spender_avg = spender_total / spender_count

spender_df['Purchase Count'] = spender_count
spender_df['Average Purchase Price'] = spender_avg
spender_df['Total Purchase Value'] = spender_total

# Create summary data frame
spender_df = spender_df.drop(columns=['Purchase ID', 'Age', 'Gender', 'Item ID', 'Item Name', 'Price'], axis=1)
spender_demo = spender_df.sort_values('Total Purchase Value', ascending=False).drop_duplicates(subset=['Total Purchase Value'])

# Format the floats
spender_demo['Average Purchase Price'] = spender_demo['Average Purchase Price'].map('${:,.2f}'.format)
spender_demo['Total Purchase Value'] = spender_demo['Total Purchase Value'].map('${:,.2f}'.format)
spender_demo.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># Create spender_df data frame to make 'Item ID' as index
popular_df = purchase_data.set_index('Item ID')

# Add the analysis columns to the data frame
popular_count = popular_df.groupby(['Item ID'])['Purchase ID'].count()
popular_total = popular_df.groupby(['Item ID'])['Price'].sum()
popular_price = popular_total / popular_count

popular_df['Purchase Count'] = popular_count
popular_df['Item Price'] = popular_price
popular_df['Total Purchase Value'] = popular_total

# Create summary data frame
popular_df = popular_df.drop(columns=['Purchase ID', 'Age', 'Gender', 'SN', 'Price'], axis=1)
popular_items = popular_df.sort_values('Purchase Count', ascending=False).drop_duplicates(subset=['Total Purchase Value'])

# Format the floats
popular_items['Item Price'] = popular_items['Item Price'].map('${:,.2f}'.format)
popular_items['Total Purchase Value'] = popular_items['Total Purchase Value'].map('${:,.2f}'.format)
popular_items.head(10)<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># Use the above data frame to sort by 'Total Purchase Value'
popular_items_2 = popular_df.sort_values('Total Purchase Value', ascending=False).drop_duplicates(subset=['Total Purchase Value'])

# Format the floats
popular_items_2['Item Price'] = popular_items_2['Item Price'].map('${:,.2f}'.format)
popular_items_2['Total Purchase Value'] = popular_items_2['Total Purchase Value'].map('${:,.2f}'.format)
popular_items_2.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10807823129251701, 'fraction_numerical': 0.009183673469387756, 'mean_word_length': 5.992271105826397, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '395611', 'license': 'Apache-2.0', 'n_tokens_mistral': 3661, 'n_tokens_neox': 3441, 'n_words': 1056, 'path': 'HeroesOfPymoli/HeroesOfPymoli_analysis.ipynb', 'repo_name': 'Grace-Bijun-Li/pandas-challenge', 'size': '11760'}