<jupyter_start><jupyter_text>Game Analysis Analysis of in-app purchases of a game* Calculations are done in python using the pandas library* Includes summary of purchase information and demographic breakdown of users  Insights:Male players make up the bulk of the player base representing 84.03% of all players (n=484). However, they tend to spend \\$0.40 less on average per individual female players (n=113), and $0.49 less than those that identify as other or did not disclose gender (n=15).The game is most popular amoung the 20 to 24 year old age demographic, representing 44.79% of the player base (n=258). The age group that spends the most per person are 35 to 39 year olds with an average spend of \\$4.76 (n=41).The most popular item is "Final Critic" with a total purchase value of \\$59.99 (n=13). This is closely followed by "Oathbreaker, Last Hope of the Breaking Storm" with a total purchase value of \\$50.76 (n=12).  Import pandas then load the "purchace_data.csv" file<jupyter_code># Import panda library
import pandas as pd

# File to Load
file_to_load = "purchase_data.csv"

# Read csv file of purchase data and store into Pandas data frame
purchase_df = pd.read_csv(file_to_load)

# Display first 5 rows
# purchase_df.head()<jupyter_output><empty_output><jupyter_text>Player Count* Total number of unique players<jupyter_code># total_players holds number of unique SN (screen names)
total_players = purchase_df["SN"].nunique()

# Convert to dataframe to display
players_df = pd.DataFrame({
    "Total Players": [total_players]
})
players_df<jupyter_output><empty_output><jupyter_text>Purchasing Analysis (Total) Displays a summary dataframe with the following calculations:* Number of Unique Items* Average Price	* Number of Purchases* Total Revenue<jupyter_code># Confirm data types before analysis

# Step left in but commented out
# print(purchase_df.dtypes)
# Unique number of items
unique_items = purchase_df["Item ID"].nunique()

# Average price of items
average_price = purchase_df["Price"].mean()

# Total number of purchases
total_purchase = purchase_df["Purchase ID"].count()

# Total revenue
total_revenue = purchase_df["Price"].sum()

# Convert variables into a dataframe
totals_df = pd.DataFrame({
    "Number of Unique Items": [unique_items],
    "Average Price": [average_price],
    "Number of Purchases": [total_purchase],
    "Total Revenue": [total_revenue]
})

# Change formatting for financial columns
totals_df["Average Price"] = totals_df["Average Price"].map("${:,.2f}".format)
totals_df["Total Revenue"] = totals_df["Total Revenue"].map("${:,.2f}".format)

# Display results
totals_df<jupyter_output><empty_output><jupyter_text>Gender Demographics Summary: number of unique players and percentage of total by gender<jupyter_code># Group by gender
gender_df = purchase_df.groupby(["Gender"])

# Variable with unique players based on SN (screen name)
count_gender = gender_df["SN"].nunique()

# Variable with percentage of players
percent_gender = (gender_df["SN"].nunique() / count_gender.sum()) * 100

# New dataframe with summary variables and formatting
summary_gender_df = pd.DataFrame({
    "Total Count": count_gender,
    "Percentage of Players": percent_gender.map("{:.2f}%".format)
})

# Sort by decending values
summary_gender_df = summary_gender_df.sort_values(["Total Count"], ascending=False)

# Remove index name in the corner
summary_gender_df.index.name = None

# Display new dataframe
summary_gender_df<jupyter_output><empty_output><jupyter_text>Purchasing Analysis (Gender) Purchasing calculations aggregated by gender:* Purchase Count* Average Purchase Price* Total Purchase Value* Avg Total Purchase per Person<jupyter_code># Uses the gender_df created in previous step (dataframe grouped by gender)

# Purchase Count
count_purchase_gender = gender_df["Purchase ID"].count()

# Average Purchase Price
avg_price_gender = gender_df["Price"].mean()

# Total Purchase Value
total_price_gender = gender_df["Price"].sum()

# Avg Total Purchase per Person
avg_price_per_person_gender = total_price_gender / count_gender

# Place variables into dataframe and format price numbers
purchase_gender_df = pd.DataFrame({
    "Purchase Count": count_purchase_gender,
    "Average Purchase Price": avg_price_gender.map("${:,.2f}".format),
    "Total Purchase Value": total_price_gender.map("${:,.2f}".format),
    "Avg Total Purchase per Person": avg_price_per_person_gender.map("${:,.2f}".format)
})

# Display dataframe
purchase_gender_df<jupyter_output><empty_output><jupyter_text>Age DemographicsSummary: number of unique players and percentage of total by gender<jupyter_code># Bin values for age
bins = [0, 9, 14, 19, 24, 29, 34, 39, 200]

age_names = ["<10", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40+"]

# Add new column to dataframe with age ranges
purchase_df["Age Ranges"] = pd.cut(purchase_df["Age"], bins, labels=age_names, include_lowest=True)

# Group by age ranges
age_df = purchase_df.groupby(["Age Ranges"])

# Count by age range based on unique screen name
count_age = age_df["SN"].nunique()

# Percentage of players based on age
percent_age = (age_df["SN"].nunique() / count_age.sum()) * 100

# New dataframe with summary variables and formatting

summary_age_df = pd.DataFrame({
    "Total Count": count_age,
    "Percentage of Players": percent_age.map("{:.2f}%".format)
})

# Remove index name in the corner
summary_age_df.index.name = None

# Display new dataframe
summary_age_df<jupyter_output><empty_output><jupyter_text>Purchasing Analysis (Age) Purchasing calculations aggregated by age ranges:* Purchase Count* Average Purchase Price* Total Purchase Value* Avg Total Purchase per Person<jupyter_code># Uses the age_df created in previous step (dataframe grouped by age ranges)

# Purchase Count
count_purchase_age = age_df["Purchase ID"].count()

# Average Purchase Price
avg_price_age = age_df["Price"].mean()

# Total Purchase Value
total_price_age = age_df["Price"].sum()

# Avg Total Purchase per Person
avg_price_per_person_age = total_price_age / count_age

# Place variables into dataframe and format price numbers
purchase_age_df = pd.DataFrame({
    "Purchase Count": count_purchase_age,
    "Average Purchase Price": avg_price_age.map("${:,.2f}".format),
    "Total Purchase Value": total_price_age.map("${:,.2f}".format),
    "Avg Total Purchase per Person": avg_price_per_person_age.map("${:,.2f}".format)
})

# Add index name in the corner
purchase_age_df.index.name = "Age Ranges"

# Display dataframe
purchase_age_df<jupyter_output><empty_output><jupyter_text>Top Spenders Purchasing calculations aggregated by SN to find the top spenders:* Purchase Count* Average Purchase Price* Total Purchase Value (sorted from largest to smallest)<jupyter_code># Group by SN (screen name)
sn_df = purchase_df.groupby(["SN"])

# Purchase Count
count_purchase_sn = sn_df["Purchase ID"].count()

# Average Purchase Price
avg_price_sn = sn_df["Price"].mean()

# Total Purchase Value
total_price_sn = sn_df["Price"].sum()

# Place variables into dataframe
purchase_sn_df = pd.DataFrame({
    "Purchase Count": count_purchase_sn,
    "Average Purchase Price": avg_price_sn,
    "Total Purchase Value": total_price_sn,
})

# Sort by decending values
purchase_sn_df = purchase_sn_df.sort_values("Total Purchase Value", ascending=False)

# Add formatting (will change datatype to string)
purchase_sn_df["Average Purchase Price"] = purchase_sn_df["Average Purchase Price"].map("${:,.2f}".format)
purchase_sn_df["Total Purchase Value"] = purchase_sn_df["Total Purchase Value"].map("${:,.2f}".format)

# Display dataframe (top 5 rows)
purchase_sn_df.head()<jupyter_output><empty_output><jupyter_text>Most Popular Items Purchasing calculations aggregated by Item ID and Item Name to find most popular items:* Purchase Count (sorted from largest to smallest)* Item Price* Total Purchase Value<jupyter_code># Extract Item ID, Item Name, and Price columns to a new dataframe
purchase_extract_df = purchase_df[["Item ID", "Item Name", "Price"]]

# Group by Item ID and Item Name
popular_items = purchase_extract_df.groupby(["Item ID", "Item Name"])

# Purchase Count
count_purchase_popular = popular_items["Item ID"].count()

# Item Price (can use mean)
item_price = popular_items["Price"].mean()

# Total Purchase Value
total_price_popular = popular_items["Price"].sum()

# Place variables into dataframe
popular_df = pd.DataFrame({
    "Purchase Count": count_purchase_popular,
    "Item Price": item_price,
    "Total Purchase Value": total_price_popular
})

# Sort by purchase count - descending values
popular_count_sorted_df = popular_df.sort_values("Purchase Count", ascending=False)

# Add formatting (will change datatype to string)
popular_count_sorted_df["Item Price"] = popular_count_sorted_df["Item Price"].map("${:,.2f}".format)
popular_count_sorted_df["Total Purchase Value"] = popular_count_sorted_df["Total Purchase Value"].map("${:,.2f}".format)

# Display dataframe (top 5 rows)
popular_count_sorted_df.head()<jupyter_output><empty_output><jupyter_text>Most Profitable Items Purchasing calculations aggregated by Item ID and Item Name to find most popular items:* Purchase Count* Item Price* Total Purchase Value (sorted from largest to smallest)<jupyter_code># Sort by above dataframe by total purchase value - descending values
popular_total_sorted_df = popular_df.sort_values("Total Purchase Value", ascending=False)

# Add formatting (will change datatype to string)
popular_total_sorted_df["Item Price"] = popular_total_sorted_df["Item Price"].map("${:,.2f}".format)
popular_total_sorted_df["Total Purchase Value"] = popular_total_sorted_df["Total Purchase Value"].map("${:,.2f}".format)

# Display dataframe (top 5 rows)
popular_total_sorted_df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10748952692346991, 'fraction_numerical': 0.012363339123326862, 'mean_word_length': 6.154970760233918, 'pattern_counts': {'":': 23, '<': 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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '430807', 'license': 'MIT', 'n_tokens_mistral': 3184, 'n_tokens_neox': 2974, 'n_words': 952, 'path': 'game-analysis.ipynb', 'repo_name': 'Seltsam1/pandas-analysis-game-data', 'size': '9787'}