# 5 Best Ways to Count Frequency of Highest Frequent Elements in Python

Rate this post

π‘ Problem Formulation: When working with data in Python, a common problem is determining how often the most frequently occurring elements appear in a list. For instance, given the input list `[3, 1, 4, 4, 5, 5, 5, 3, 3]`, the desired output is `3`, as the number `3` and the number `5` occur three times each and are the most frequent elements in the list.

## Method 1: Using collections.Counter

The `collections.Counter` class in Python is specifically designed for counting hashable objects. It’s part of Python’s standard library and represents a high-performance counting solution which internally uses a dictionary to store elements and their counts. This method is highly efficient and easy to implement for finding the frequency of the most common elements.

Here’s an example:

```from collections import Counter

def highest_frequency_count(nums):
counts = Counter(nums)
max_count = max(counts.values())
return max_count

nums = [3, 1, 4, 4, 5, 5, 5, 3, 3]
print(highest_frequency_count(nums))
```

Output:

`3`

This code snippet defines a function `highest_frequency_count` that takes a list of numbers and returns the frequency of the most common elements. The `Counter` class creates a counts dictionary with elements as keys and their counts as values. Then, `max(counts.values())` returns the highest frequency.

## Method 2: Using pandas’ value_counts

pandas is a popular data manipulation library that provides the `value_counts()` method to count the frequency of unique values in a column of a DataFrame or a Series object. It’s an excellent choice when you’re already using pandas for data analysis, providing straightforward and efficient operations with data structures.

Here’s an example:

```import pandas as pd

def highest_frequency_count_pandas(nums):
return pd.Series(nums).value_counts().iat[0]

nums = [3, 1, 4, 4, 5, 5, 5, 3, 3]
print(highest_frequency_count_pandas(nums))
```

Output:

`3`

In the code snippet, `pd.Series(nums)` creates a pandas Series from the list of numbers. The `value_counts()` method returns a series containing counts of unique elements, sorted in descending order. `iat[0]` accesses the first item, which corresponds to the highest frequency.

## Method 3: Using max and list comprehension

This involves utilizing Python’s built-in `max` function together with a list comprehension. This method is useful for simple lists and when you prefer not to import additional libraries. It’s straightforward and uses only built-in Python features, but may not be the most efficient for large datasets.

Here’s an example:

```nums = [3, 1, 4, 4, 5, 5, 5, 3, 3]

def highest_frequency_count_simple(nums):
return max([nums.count(i) for i in set(nums)])

print(highest_frequency_count_simple(nums))
```

Output:

`3`

This snippet defines a function `highest_frequency_count_simple` that takes a list of numbers and returns the frequency of the most common element. It generates a list of counts for each unique element in `nums` using `[nums.count(i) for i in set(nums)]`, and then finds the maximum value with `max()`.

## Method 4: Using numpy library

numpy is another powerful library for numerical computing. By using the `numpy.unique()` function, you can easily count the frequency of each element in an array and identify the highest frequency. This approach is very efficient, especially for large arrays or when you are already making use of numpy for other calculations.

Here’s an example:

```import numpy as np

def highest_frequency_count_numpy(nums):
unique, counts = np.unique(nums, return_counts=True)
return counts.max()

nums = np.array([3, 1, 4, 4, 5, 5, 5, 3, 3])
print(highest_frequency_count_numpy(nums))
```

Output:

`3`

The function `highest_frequency_count_numpy` converts the list to a numpy array and then uses `np.unique()` with the `return_counts` parameter set to `True` to return the counts of unique values. `counts.max()` is used to find the maximum frequency among the counts.

## Bonus One-Liner Method 5: Using a Lambda and max

This is a quick and concise one-liner approach using `lambda` functions and `max()`, combined with the counting capability of `list.count()`. It is pleasing for its simplicity and readability for Python enthusiasts who prefer functional programming patterns.

Here’s an example:

```nums = [3, 1, 4, 4, 5, 5, 5, 3, 3]

highest_frequency_count_oneliner = lambda nums: max(nums.count(num) for num in set(nums))
print(highest_frequency_count_oneliner(nums))
```

Output:

`3`

This concise piece of code defines a `lambda` function which does everything in one line. It iterates over the unique elements in the list, counts the occurrences of each, and uses `max()` to find the highest frequency.

## Summary/Discussion

• Method 1: collections.Counter. Highly efficient for counting in Python; part of the standard library; returns a dictionary of counts. Not as efficient for very large datasets.
• Method 2: pandas’ value_counts. Ideal for pandas users and handling large datasets; provides quick and direct access to counts. Requires importing pandas, which can be overkill for simple tasks.
• Method 3: max and list comprehension. Simple to use with built-in functions; no third-party libraries needed. Can be slow and inefficient for large lists.
• Method 4: numpy library. Best suited for numerical data and large arrays; integrates well with other numpy operations. Requires the numpy library.
• Method 5: Lambda and max. A neat, functional programming one-liner. Clear and concise but may not be as efficient as other methods for large datasets.