Python for Data Science Fundamentals: Assignment 1 : Freq Tables

Hello there, I’m having a hard time understanding the code in Page 10/14 of Assignment 1: Most Common Apps by Genre: Part Two.

Specifically I don’t understand how the dictionary ‘table’ gets populated with the appropriate key:value pairs. Which line actually stores the keys and which line stores the values?

Thanks in advance.

Here is the code I’m having trouble with:

def freq_table(dataset, index):
table = {}
total = 0

for row in dataset:
    total += 1
    ***value = row[index]***  (Can someone explain what this variable 'value' is storing?)
    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])

Hey, I’ll give it a shot.

value variable is created and assigned to 'row[index]. So every time it loops through an item in the dataset, it increases total by 1 and assigns that datasets row[index] as a key with no value. Then it looks through your table dictionary for the value variable from that row. If it exist, it gives it a value of +1, if not, it gives it a value of 1.

The key is the value[index], whereas the value starts out as 0 and becomes 1/increases by 1.

Thanks mikemason1987. I understand it a little better now. However, you mention “assigns that datasets row[index] as a key with no value”.

Which line in the code assigns the dataset’s row[index] as a key with no value to the “table” dictionary? I don’t see where that happens. I see the row[index] being stored in a variable called “value”. And then in the next line it is checking to see if the contents of the “value” variable exists in the “table” dictionary.

Thanks for your help.

Yea, disregard what I said, dictionaries can be created with blank values, but those have to be denoted with ‘’. (that’s 2 quotes).

With the function, it requires 2 inputs from you, a dataset value and index number, when you call it

The for loop goes through each item in the data set, with index being a number (it’s been a while but probably a specific item number i.e item number 5/6/etc in the row of data in dataset.

value = row[index] means we are assigning the variable ‘value’ to the index number we use for that row.

Say for example I have a table of tables pointing out an individual’s favorite breakfast, lunch, dinner. It would look something akin to this: (top block which you would splice out would be [name, breakfast, lunch, dinner]):
dataset = [[‘Bill Murray’, ‘pancakes’, ‘steak’, ‘lobster’], [‘Jimmy the Snitch’, ‘Ice Cream’, ‘Soylent Green’, ‘bread’],[‘Pinochio’, ‘Snickers’, ‘Milky Way’, ‘Banana’], [‘Jersey Devil’,‘pancakes’,‘JP8’,‘noodles’]]

  1. So now, for each row in dataset (meaning it’ll loop through each inner table of our dataset table).

  2. Then it will increase the total variable by 1.

  3. Next, lets say we want to get the frequency of breakfasts in our dataset, we would use 1 as our splice (name being 0, breakfast being 1, lunch 2, dinner 3). So the value variable is assigned row[1], which we used when we called that function and gave it that parameter (table = freq_table(dataset,index).

  4. Next our If-then-else statement kicks in. We have it look in the table dictionary file for a key that matches the breakfast item (in the case of first run, it looks in the blank dictionary for a key called ‘pancakes’. Since it wont be in there, it will add ‘pancakes’ to the dictionary ([table[pancakes]) and assign it a value of 1 (making it {‘pancakes’: 1} in the dictionary. It will continue to loop through the rest of the data sets adding those items and assigning 1 to their value. Then when it gets to the last item in the dataset, it will find that ‘pancakes’ already exist in the table, so it will increase its value by 1, making the dictionary ready like:
    {‘pancakes’:2, ‘Ice Cream’:1,‘Snickers’:1}

I hope that clears it up for you.

mikemason1987, wow, thank you for that detailed explanation. It took me some time but that certainly cleared up the fog. Now I get how it adds {‘pancakes’:2} to the dictionary “table”.

Many thanks for taking the time to help out a newbie!

Hey @tsg,

We have a solved feature that allows you the ability to mark something as the “correct” answer, which helps future students with the same question quickly find the solution they’re looking for.

Here’s an article on how to mark posts as solved - I don’t want to do this for you until I know that solution/explanation works.

Best,
Alvin.

why the function below does not return anything when I call the function and print it?

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