<jupyter_start><jupyter_text>Pandas Challenge  HeroesOfPymoli_starter<jupyter_code># Dependencies and Set Up
import pandas as pd
import os
#Importing File
pd.read_csv("purchase_data.csv")
#Set Up purchase_data to store data
purchase_data_pd = pd.read_csv("purchase_data.csv")
purchase_data_pd.head()
# Review Dataframe format
purchase_data_pd.dtypes
#Format Purchase ID  to Integer 
purchase_id_pd =purchase_data_pd["Purchase ID"].astype("int")
purchase_id_pd.head()<jupyter_output><empty_output><jupyter_text>Player Count - Display the total number of players<jupyter_code>#Display total player
total_player = purchase_data_pd["SN"].value_counts().count()
players_df = pd.DataFrame({"Total Players": [total_player]})
players_df
#Purchasing Analysis (Total)
purchase_data_pd.describe()
# Calculate unique items
unique_items = purchase_data_pd["Item ID"].value_counts().count()
unique_items
# Calculate total revenue
total_revenue = purchase_data_pd["Price"].sum()
total_revenue
#Run Basic Calculation
number_purchases = purchase_data_pd["Purchase ID"].value_counts().sum()
number_purchases
#Calculate average price
average_price = purchase_data_pd["Price"].mean()
average_price<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- Displayed data cleaner formatting- Display the summary data frame<jupyter_code>#Create Summary Frame to hold results 
basic_calculations_df = pd.DataFrame({"Number Unique Items": [unique_items],"Average Price": [average_price],"Number of Purchases":[number_purchases], "Total Revenue":[total_revenue]})
basic_calculations_df.head().style.format({"Average Price": "${:,.2f}","Total Revenue": "${:,.2f}"})
# Display Gender and Player Columns
purchase_data_pd.columns
#Create a new dataframe to hold gender and students 
gender_students_df = purchase_data_pd.loc[:, ["SN", "Gender"]]
gender_students_df.head()
#Check for duplicates
duplicate_students = gender_students_df[gender_students_df.duplicated()].count()
duplicate_students<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 Gender as index and extract unique values 
grouped_gender_df = gender_students_df.groupby(["Gender"])
unique_gender= grouped_gender_df.nunique()
unique_gender["SN"]
# create a new column and set it equal the the output of the calculation that we'd like to perform
unique_gender['Percent of Players'] = (unique_gender["SN"]/ total_player*100).map("{:,.2f}%".format)
unique_gender.head()
#Rename SN Column
unique_gender = unique_gender.rename(columns={"SN": "Total Count"})
unique_gender[["Total Count", "Percent of Players"]].head()
# Extract purchase count per gender
grouped_gender_df = purchase_data_pd.groupby(["Gender"])
purchase_gender = grouped_gender_df["Purchase ID"].nunique()
purchase_gender
#Create Purchase Gender into Dataframe
purchase_gender_df = pd.DataFrame(purchase_gender)
purchase_gender_df = purchase_gender_df.rename(columns={"Purchase ID": "Purchase Count"})
purchase_gender_df
#Calculate Total Purchase Value
value_gender = grouped_gender_df["Price"].sum()
value_gender
#Calculate Average Purchase Price
average_price = value_gender / purchase_gender
average_price.head()
# Add Average Purchase Price to Dataframe
purchase_gender_df['Average Purchase Price'] = average_price.map("${:,.2f}".format)
purchase_gender_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<jupyter_code># Add Total Purchase Value  to Dataframe
purchase_gender_df['Total Purchase Value'] = value_gender.map("${:,.2f}".format)
purchase_gender_df
#Create variable for numbers per gender
grouped_gender_df = gender_students_df.groupby(["Gender"])
unique_gender= grouped_gender_df.nunique()
gender_numbers = unique_gender["SN"]
gender_numbers
# Add Total Purchase Value  to Dataframe
purchase_gender_df['Avg Total Purchase per Person'] = (value_gender/gender_numbers).map("${:,.2f}".format)
purchase_gender_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<jupyter_code>#Create Data frame for existing players by age
age_players_df = purchase_data_pd.loc[:, ["SN", "Age"]]
age_players_df.head()<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<jupyter_code>#Print min and max
print(age_players_df["Age"].max())
print(age_players_df["Age"].min())
#Create bins for ages
bins = [0,9,14,19,24,29,34,39,45]
#Create the name fo the bins
label_names = ["<10", "10-14", "15-19", "20-24", "25-29","30-34","35-39","40+"]
#Categorize the existing players using the age bins
age_players_df["Age Group"] = pd.cut(age_players_df["Age"], bins, labels=label_names)
age_players_df.head
#Group by group age
age_group = age_players_df.groupby("Age Group")
age_count = age_group['SN'].nunique()
age_demographics_df = pd.DataFrame(age_count)
age_demographics_df.head()
#Rename SN Column
age_demographics_df= age_demographics_df.rename(columns={"SN": "Total Count"})
age_demographics_df.head()
#Calculate Percentage of Players 
age_demographics_df['Percent of Players']= (age_count/total_player*100).map("{:,.2f}%".format)
age_demographics_df.head(8)
#Create reduce dataframe
reduce_players_df = purchase_data_pd.loc[:, ["SN", "Age","Price", "Item ID", "Purchase ID"]]
reduce_players_df.head()
#Create bins for ages
bins = [0,9,14,19,24,29,34,39,45]
#Create the name fo the bins
label_names = ["<10", "10-14", "15-19", "20-24", "25-29","30-34","35-39","40+"]
#Categorize the existing players using the age bins
reduce_players_df["Age Group"] = pd.cut(reduce_players_df["Age"], bins, labels=label_names)
reduce_players_df
#Age Group by Purchase ID
age_price_group = reduce_players_df.groupby("Age Group")
item_count = age_price_group['Purchase ID'].nunique()
item_count
#Create Dataframe for Purchase Analysis 
purchase_analysis_df = pd.DataFrame(item_count)
purchase_analysis_df.head()
#Calculate Total Purchase Value
value_age = age_price_group["Price"].sum()
value_age
#Calculate Average Purchase Price
average_age = value_age / item_count
average_age.head()
# Add Average Purchase Price to Dataframe
purchase_analysis_df['Average Purchase Price'] = average_age.map("${:,.2f}".format)
purchase_analysis_df
# Add Total Purchase Price to Dataframe
purchase_analysis_df['Total Purchase Price'] = value_age.map("${:,.2f}".format)
purchase_analysis_df
# Add Avg Total Purchase per Person to Dataframe
purchase_analysis_df['Total Purchase per Person'] = (value_age/age_count).map("${:,.2f}".format)
purchase_analysis_df<jupyter_output><empty_output><jupyter_text>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  Calculations<jupyter_code># Total Price per SN 
sn_total= reduce_players_df.groupby("SN").sum()["Price"].rename("Total Purchase Value")
# Average Price per SN 
sn_average= reduce_players_df.groupby("SN").mean()["Price"].rename("Average Purchase Price")
# Count Price per SN
sn_count= reduce_players_df.groupby("SN").count()["Price"].rename("Purchase Count")<jupyter_output><empty_output><jupyter_text>Create DataFrame<jupyter_code>sn_df= pd.DataFrame({"Total Purchase Value": sn_total, "Average Purchase Price": sn_average, "Purchase Count": sn_count})<jupyter_output><empty_output><jupyter_text>Sort the total purchase value column in descending order<jupyter_code>sorted_sn= sn_df.sort_values("Total Purchase Value", ascending= False)
sorted_sn["Total Purchase Value"].max<jupyter_output><empty_output><jupyter_text>Map Total Purchase Value and Average Purchase Price to Currency<jupyter_code>sorted_sn["Total Purchase Value"]= sorted_sn["Total Purchase Value"].map("${:,.2f}".format)
sorted_sn["Average Purchase Price"]= sorted_sn["Average Purchase Price"].map("${:,.2f}".format)<jupyter_output><empty_output><jupyter_text>Display only top 5<jupyter_code>sorted_sn.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  Retrieve the Item ID, Item Name, and Item Price columns<jupyter_code>items_pd= purchase_data_pd.loc[:,["Item ID", "Item Name", "Price"]]<jupyter_output><empty_output><jupyter_text>Calculations<jupyter_code>## Total Price per id
id_total= items_pd.groupby(["Item ID", "Item Name"]).sum()["Price"].rename("Total Purchase Value")
## Average Price per id
id_average= items_pd.groupby(["Item ID", "Item Name"]).mean()["Price"].rename("Item Price")
## Number of purchases  per id
id_count= items_pd.groupby(["Item ID", "Item Name"]).count()["Price"].rename("Purchase Count")<jupyter_output><empty_output><jupyter_text>Create DataFrame<jupyter_code>id_df= pd.DataFrame({"Purchase Count": id_count,"Total Purchase Value":id_total, "Item Price": id_average })<jupyter_output><empty_output><jupyter_text>Sort Values by Purchase Count<jupyter_code>sorted_id_df= id_df.sort_values("Purchase Count", ascending= False)
sorted_id_df.head()<jupyter_output><empty_output><jupyter_text>Map Total Purchase Value and Average Purchase Price to currency<jupyter_code>sorted_id_df["Total Purchase Value"]= sorted_id_df["Total Purchase Value"].map("${:,.2f}".format)
sorted_id_df["Item Price"]= sorted_id_df["Item Price"].map("${:,.2f}".format)<jupyter_output><empty_output><jupyter_text>Display top 5 Items<jupyter_code>top_5= sorted_id_df[:5]
top_5<jupyter_output><empty_output><jupyter_text>Most Profitable Items - Sort the above table by total purchase value in descending order<jupyter_code>prof_items= id_df.sort_values("Total Purchase Value", ascending= False)
prof_items["Total Purchase Value"]= prof_items["Total Purchase Value"].map("${:,.2f}".format)
prof_items["Item Price"]= prof_items["Item Price"].map("${:,.2f}".format)
top_5_prof = prof_items.head()
top_5_prof<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12105997210599721, 'fraction_numerical': 0.010971641097164109, 'mean_word_length': 8.304498269896193, 'pattern_counts': {'":': 16, '<': 79, '<?xml version=': 0, '>': 77, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '492125', 'license': 'ADSL', 'n_tokens_mistral': 3626, 'n_tokens_neox': 3430, 'n_words': 883, 'path': 'HeroesOfPymoli/HeroesOfPymoli.ipynb', 'repo_name': 'Elisu87/pandas-challange', 'size': '10755'}