String Index out of range Error Profitable Apps guided project

Appprofit.ipynb (21.7 KB)

Screen Link: https://app.dataquest.io/m/428/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/10/most-common-apps-by-genre-part-two

Your Code:

android_english=[]
ios_english=[]

for app in android_clean:
    name=app[0]
    if is_english(name):
        android_english.append(app)
        
for app in ios:
    name=app[1]
    if is_english(name):
        ios_english.append(app)
        
explore_data(android_english, 0, 4, True)
print('\n')
explore_data(ios_english, 0, 4, True)

android_engfree = []
ios_engfree = []

for app in android_english:
    price = app[7]
    if price == '0':
        android_engfree.append(price)
        
for app in ios_english:
    price = app[4]
    if price == '0.0':
        ios_engfree.append(price)
        
print (len(android_engfree))
print (len(ios_engfree))

def freq_table(dataset, index):
    table = {}
    total = 0
    
    for row in dataset:
        total += 1
        value = row[index]
        if value in table:
            table[value] += 1
        else:
            table[value] = 1
            
    table_percentage = {}
    for key in table:
        percentage = (table[key] / total) * 100
        table_percentage[key] = percentage
        
    return table_percentage

def display_table(dataset, index):
    table = freq_table(dataset, index)
    table_display = []
    for key in table:
        key_val_as_tuple = (table[key], key)
        table_display.append(key_val_as_tuple)
        
    table_sorted = sorted(table_display, reverse = True)
    for entry in table_sorted:
        print(entry[1], ':', entry[0])

freq_table(ios_engfree, -5) ```

What I expected to happen: According to the solution guide for the python course, after running the freq_table(ios_engfree, -5) it should have resulted in generating a frequency table like this:
Games : 58.16263190564867
Entertainment : 7.883302296710118
Photo & Video : 4.9658597144630665
Education : 3.662321539416512
Social Networking : 3.2898820608317814
Shopping : 2.60707635009311
Utilities : 2.5139664804469275
..... etc, etc, etc

What actually happened: get an error code no matter what I try to do:

IndexErrorTraceback (most recent call last)
in ()
----> 1 freq_table(ios_engfree, -5)

in freq_table(dataset, index)
5 for row in dataset:
6 total += 1
—> 7 value = row[index]
8 if value in table:
9 table[value] += 1

IndexError: string index out of range


Other details: This is my first guided project and first time using Jupyter Notebook.  I have tried even copying and pasting the solution into my note book and it still gives the error.  I know that I have a few different names to variables, I want to see that I am able to change a few things and still get the same results.  Which I have up to this point.  Can anyone help me identify how to fix this error?  Thanks so much

Hi @amhackdpt2. If you inspect ios_engfree in a separate cell using ios_engfree[:5], you’ll see that it’s a list of just the prices instead of the data rows.

['0.0', '0.0', '0.0', '0.0', '0.0']

Going back to where it was created, we find the error in this section of code:

for app in android_english:
    price = app[7]
    if price == '0':
        android_engfree.append(price)   # appending price, not the row
        
for app in ios_english:
    price = app[4]
    if price == '0.0':
        ios_engfree.append(price)       #appending price, not the row

Once you fix that error, rerun the cells, and you should get the expected results.

@april.g Thank you so much for the help! It was driving me crazy. I can’t believe I didn’t see that now that it was pointed out. I am still very new to this. I really appreciate it.

1 Like