Is there an easier way? 7. Creating Frequency Tables

To determine frequency of all distinct items in column 11, lessons 6 + 7 tell us to write this code. Is there an easier way to say: “count all distinct items in column 11”?

def extract(index): 
    column = []
    for row in apps_data[1:]: 
        value=row[index]
        column.append(value)
    return column

genres = extract(11) 

def freq_table(column):
    frequency_table = {}    
    for value in column:
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1            
    return frequency_table

genres_ft = freq_table(genres)

You will learn about using pandas.Series.value_counts to create a frequency table in the coming panda tutorial

2 Likes

Sometimes the same goal can be achieved by pure python, numpy, pandas, sklearn functions. Eg. One Hot Encoding. The differences are in speed, subtleties about data types, and API matching (eg. using sklearn for proprocessing so you can use sklearn.pipeline.Pipeline)