Count the frequencies in a list in Python

When it comes to data analysis, one of the most common operations you can perform on a list is to count the item frequencies. This operation will return the unique values of your list and the number of times they appear.

Let’s see how we can perform this operation using different approaches.

Use a dictionary to count frequencies of items in a list

We can loop our list and create a frequency dictionary using simple Python code. The logic is straightforward:

#our list items = ['apple', 'apple', 'banana', 'banana', 'orange'] #our frequency dict freq = {} #we loop our list for item in items: #if item already a key if item in freq: #add count by one freq[item] += 1 #if item not a key yet else: #create key and assign a value of 1 freq[item] = 1 for key, value in freq.items(): print(key, value)
Code language: Python (python)

Output:

apple 2 banana 2 orange 1

We can also leverage the count() method to simplify our code:

#our list items = ['apple', 'apple', 'banana', 'banana', 'orange'] #our frequency dict freq = {} #we loop our unique elements of items for item in set(items): #we count the number of occurrences freq[item] = items.count(item) for key, value in freq.items(): print(key, value)
Code language: PHP (php)

Output:

apple 2 banana 2 orange 1

Use collections to count the frequencies of elements in a list

The collection library comes with a Counter() object that allows us to perform the same operations with less code. You must understand the codes we included above to avoid using “black box” without understanding how they word, though.

import collections #our list items = ['apple', 'apple', 'banana', 'banana', 'orange'] #our frequency dict freq = collections.Counter(items) print(freq)
Code language: Python (python)

Output:

Counter({'apple': 2, 'banana': 2, 'orange': 1})
Code language: Python (python)

Use Pandas to count the frequencies of elements in a list

Finally, you can leverage the value_counts() method from the Pandas library. I recommend doing it like only if you are already working with a DataFrame. Otherwise, you’ll have to load your list into a Pandas Series, apply the change, and export to a list again.

import pandas as pd df = {'words':['apple', 'apple', 'banana', 'banana', 'orange']} df = pd.DataFrame(df, columns=['words']) result = df['words'].value_counts() print(result)
Code language: JavaScript (javascript)

Output:

apple2
banana2
orange1

Leave a Reply

Your email address will not be published. Required fields are marked *