# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Profitable App Profiles for the App Store and Google Play Markets
# This project is to find mobile app profiles that are profitable for the App Store and Google Play markets. 

import pandas as pd
from csv import reader

# # Slicing and Exploring Data

# +
##slicing data

data1=open("AppleStore.csv")
data2=open("googleplaystore.csv")
applereader=reader(data1)
applelist=list(applereader)

googlereader=reader(data2)
googlelist=list(googlereader)

appleheader=applelist[0]
androidheader=googlelist[0]
ios=applelist[1:]
android=googlelist[1:]


# -

def explore_data(dataset, start, end, rows_and_columns=False):
    dataset_slice = dataset[start:end]    
    for row in dataset_slice:
        print(row)
        print('\n') # adds a new (empty) line after each row

    if rows_and_columns:
        print('Number of rows:', len(dataset))
        print('Number of columns:', len(dataset[0]))


explore_data(applelist,0,3,True)

explore_data(android,2,100,True)

# # DATA CLEANING STEP 1: get rid of errors and duplicate values

###as this has error in its number of reviews: gives error after running through the loop while converting string to float 
del android[10472]

androidheader

# +
####Finding Duplicate Value
##FOR ANDROID APPS:
duplicate_apps = []
unique_apps = []

for app in android:
    name = app[0]
    if name in unique_apps:
        duplicate_apps.append(name)
    else:
        unique_apps.append(name)
    
print('Number of duplicate android apps:', len(duplicate_apps))
print('\n')
print('Examples of duplicate apps:', duplicate_apps[:15])


print("\n")

##FOR IOS Apps

ios_duplicate_apps = []
ios_unique_apps = []

for app in ios:
    name = app[1]
    if name in ios_unique_apps:
        ios_duplicate_apps.append(name)
    else:
        ios_unique_apps.append(name)
    
print('Number of duplicate ios apps:', len(ios_duplicate_apps))
print('\n')
print('Examples of duplicate apps:', ios_duplicate_apps[:15])


# +
##Validating data cleaning
print("Total number of android apps",len(android))
print("Number of duplicate android apps:",len(duplicate_apps))
print("Expected number of android apps after removing duplicated",len(android)-1181)

print("\n")


print("Total number of ios apps",len(ios))
print("Number of duplicate ios apps:",len(ios_duplicate_apps))
print("Expected number of ios apps after removing duplicated",len(ios)-2)




# -

# There might be duplicate data values with same or less number of reviews,we need data with highest number of reviews and eliminate the duplicate values with less reviews:

# +
##android
reviews_max = {}

for app in android:
    name = app[0]
    n_reviews = float(app[3])
    
    if name in reviews_max and reviews_max[name] < n_reviews:
        reviews_max[name] = n_reviews
        
    elif name not in reviews_max:
        reviews_max[name] = n_reviews

# +
###ios
reviews_ios_max = {}

for app in ios:
    name = app[1]
    n_reviews = float(app[5])
    
    if name in reviews_ios_max and reviews_ios_max[name] < n_reviews:
        reviews_ios_max[name] = n_reviews
        
    elif name not in reviews_ios_max:
        reviews_ios_max[name] = n_reviews

# -

len(reviews_ios_max)

# +
android_clean_data=[]
already_added=[]

for app in android:
    name = app[0]
    n_reviews = float(app[3])
    
    if (reviews_max[name] == n_reviews) and (name not in already_added):
        android_clean_data.append(app)
        already_added.append(name)
# -

len(android_clean_data)

# +
ios_clean_data=[]
ios_already_added=[]

for app in ios:
    name = app[1]
    n_reviews = float(app[5])
    
    if (reviews_ios_max[name] == n_reviews) and (name not in ios_already_added):
        ios_clean_data.append(app)
        ios_already_added.append(name)
# -

len(ios_clean_data)
###now this seems a satisfactory data cleansing 


# # DATA CLEANING SETP 2: Filtering out english only apps
#

#
# This can be done using the ord build in function where the fuction will output the value of the ASCII number corresponding to the name.
# We can eliminate non-english names by using ASCII number
# i.e if the name contains other characters than that in english library it will give a value higher than 127 whic indicates its a non english word.
#
# Also I have given the names list the liberty to have 3 Ascci characters.If the number has more than 3 of the ASCII characters then those names will be classified as non-english words
#

# +
def is_english(string):
    non_ascii = 0
    
    for character in string:
        if ord(character) > 127:
            non_ascii += 1
    
    if non_ascii > 3:
        return False
    else:
        return True
################################################
english_clean_android_data=[]
deldata=[]

for app in android_clean_data:
    name=app[0]
    if is_english(name)==True:
        english_clean_android_data.append(app)
    else:
        deldata.append(app)
        
print(len(english_clean_android_data))


# +
def is_english(string):
    non_ascii = 0
    
    for character in string:
        if ord(character) > 127:
            non_ascii += 1
    
    if non_ascii > 3:
        return False
    else:
        return True
################################################
english_clean_ios_data=[]
deliosdata=[]

for app in ios_clean_data:
    name=app[1]
    if is_english(name)==True:
        english_clean_ios_data.append(app)
    else:
        deliosdata.append(app)
        
print(len(english_clean_ios_data))
# -

# DATA CLEANING STEP 3: Lets filter the free apps for data analysis

#lets check the number of free apps
final_android_free=[]
final_android_paid=[]
for app in english_clean_android_data:
    typ=app[6]
    
    if typ =="Free":
        final_android_free.append(app)
    else:
        final_android_paid.append(app)

#lets check the number of free apps
final_ios_free=[]
final_ios_paid=[]
for app in english_clean_ios_data:
    typ=app[4]
    
    if typ =="0.0":
        final_ios_free.append(app)
    else:
        final_ios_paid.append(app)

# +
print("Total number of clean android apps:",len(final_android_free))

print("Total number of clean ios apps:    ",len(final_ios_free))


# -

# # DATA ANALYSIS

# # Most Common Apps by Genre
#

# +
def freqtable(dataset,index):
    table={}
    total=0
    
    for app in dataset:
        total +=1
        name=app[index]
    
        if name in table:
            table[name] += 1
        else:
            table[name]=1
    
    percentages_table={}
    ###finding percentages
    for app in table:
        percentages_table[app]=((table[app]/total)*100)
        #percentages_table.append(print(app," :",(table[app]/total)*100))
    return percentages_table
        
        
        
def display_table(dataset,index):
    d_table=freqtable(dataset,index)
    table_display=[]
    
    for app in d_table:
        tt=(d_table[app],app)
        table_display.append(tt)
    
    sorted_Table=sorted(table_display,reverse=True)
    
    for i in sorted_Table:
        print(i[1],": ",i[0])
    
            
        
    
    
    
# -

##frequency table for android apps based on genre
display_table(final_android_free,9)

# +
##frequency table for android apps based on type

display_table(final_android_free,1)
# -

# # iOS DATA

# +
##frequency table for ios apps

display_table(final_ios_free,11)
# -

# Calculating average number of installs for each genre of App Store apps depending upon the toatal number of user ratings

# +

average_ios={}
for app in final_ios_free:
    genre=app[-5]
    ratings=app[5]
    if genre in average_ios:
        average_ios[genre] += float(ratings)
    else:
        average_ios[genre]=float(ratings)

freq_ios=freqtabletemp(final_ios_free,11)

for app in average_ios:
    print(app,":",average_ios[app]/freq_ios[app])
# -

# On average, navigation apps have the highest number of user reviews, but this figure is heavily influenced by Waze and Google Maps, which have close to half a million user reviews together:
#
#

for app in final_ios_free:
    if app[-5] == 'Navigation':
        print(app[1], ':', app[5])

# Calculating average number of installs for each genre of Google Play Store apps depending upon the toatal number of Installs

# # ANDROID DATA

# +
average_android={}
import re
for app in final_android_free:
    genre=app[1]
    
    installs=app[5].replace(',','')
    installs=installs.replace('+','')
    installs=float(installs)
    
    if genre in average_android:
        average_android[genre] += float(installs)
    else:
        average_android[genre]= float(installs)

freq_android=freqtabletemp(final_android_free,1)

for app in average_android:
    print(app,":",average_android[app]/freq_android[app])
# -

# On average, communication apps have the most installs: 38,456,119. This number is heavily skewed up by a few apps that have over one billion installs (WhatsApp, Facebook Messenger, Skype, Google Chrome, Gmail, and Hangouts), and a few others with over 100 and 500 million installs:
#
#

for app in final_android_free:
    if app[1] == 'COMMUNICATION' and (app[5] == '1,000,000,000+'
                                      or app[5] == '500,000,000+'
                                      or app[5] == '100,000,000+'):
        print(app[0], ':', app[5])

#
# If we removed all the communication apps that have over 100 million installs, the average would be reduced roughly ten times:
#
#
#

# +
under_100_m = []

for app in final_android_free:
    n_installs = app[5]
    n_installs = n_installs.replace(',', '')
    n_installs = n_installs.replace('+', '')
    if (app[1] == 'COMMUNICATION') and (float(n_installs) < 100000000):
        under_100_m.append(float(n_installs))
        
sum(under_100_m) / len(under_100_m)
# -

# # Conclusions
# In this project, we analyzed data about the App Store and Google Play mobile apps with the goal of recommending an app profile that can be profitable for both markets.
#
#

StarcoderdataJupyterScriptsDedupFiltered