Link to download my project (17.9 KB)
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])
and the cell after that
print(apple_header[-5])
which outputs prime_genre
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)
~\AppData\Local\Temp\ipykernel_892\439294329.py in <module>
----> 1 display_table(apple_free, -5)
~\AppData\Local\Temp\ipykernel_892\11938962.py in display_table(dataset, index)
20
21 def display_table(dataset, index):
---> 22 table = freq_table(dataset, index)
23 table_display = []
24 for key in table:
~\AppData\Local\Temp\ipykernel_892\11938962.py 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
I understand that variables inside of lists and dictionaries have index numbers to call upon them and I’m pretty sure the above code is attempting to do that. Specifically, attempting to call upon the prime_genre
column in the ios dataset (which I named apple_free for the final, cleaned dataset by the way). I even used print(apple_header[-5])
before running the freq_table function to verify that prime_genre
did in fact exist at that specific index number inside that specific dataset.
Ah well, it’s probably something obvious. Any help is appreciate!
Click here to view the jupyter notebook file in a new tab