Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds

IndexError: string index out of range

Screen Link:

My Code:

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_percentages = {}
    for key in table:
        percentage = table[key] / total * 100
        table_percentages[key] = percentage
        
    return table_percentages

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])

Anytime I try to run this display_table(ios_final, 11)

What I expected to happen:
Games : 58.16263190564867
Entertainment : 7.883302296710118
Photo & Video : 4.9658597144630665
Education : 3.662321539416512
Social Networking : 3.2898820608317814
Shopping : 2.60707635009311
Utilities : 2.5139664804469275
Sports : 2.1415270018621975
Music : 2.0484171322160147
Health & Fitness : 2.0173805090006205
Productivity : 1.7380509000620732
Lifestyle : 1.5828677839851024
News : 1.3345747982619491
Travel : 1.2414649286157666
Finance : 1.1173184357541899
Weather : 0.8690254500310366
Food & Drink : 0.8069522036002483
Reference : 0.5586592178770949
Business : 0.5276225946617008
Book : 0.4345127250155183
Navigation : 0.186219739292365
Medical : 0.186219739292365
Catalogs : 0.12414649286157665

What actually happened:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-ead3688357e4> in <module>
----> 1 display_table(ios_final, 11)

<ipython-input-30-ec8eacf2e428> in display_table(dataset, index)
     19 
     20 def display_table(dataset, index):
---> 21     table = freq_table(dataset, index)
     22     table_display = []
     23     for key in table:

<ipython-input-30-ec8eacf2e428> 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

This is Guided Project: Profitable App Profiles for the App Store and Google Play Markets, please I need someone to help me pointout where the error is from

I ran your code and it worked fine; no errors.

The difference being the function call - display_table(ios_final, 11)

You need to cross-check ios_final and see if it is as expected or not. Because in your code, this is essentially what’s happening -

for row in ios_final:
    value = row[11]

and you get the out of range error, implying that 11 is not a valid index for row. Given the dataset, that should not be possible unless there’s something wrong with ios_final

2 Likes

Like @the_doctor said, the error might be from your ios_final, try to see if the column you intended working on exit in the data set by extracting the column first.

thank you very much i have seen my mistake, indenting messed things up

welcome to the family also

Thank you, sir, I’ve seen my mistake, it was the way I indented