In this thread, you’ll learn different methods and techniques of counting frequency or the number of occurrences of unique items in a series. There are several methods to achieve this task, let’s go over a few of them:
1. Using a Python dictionary:
- This technique involves creating a dictionary and looping through all items in the series and using
get(item, default)method, we’d be updating the counts.
get(item, default)method allows you to retrieve values associated with keys in the dictionary;
itemis the key to which the value you want to find, and
defaultis the value to return if the key is not found.
2. Using "value_counts()" Pandas method:
value_counts()method returns a series containing counts of unique values in a sequence of values.
sort = Truesorts the results according to the values in descending order.
3. Using "np.unique()" method:
np.unique()method returns unique values from a sequence of values.
return_countsis set to
np.unique()function, it not only returns the unique values in an array or list but also the count of each unique value.
np.unique() returns separate lists for the unique items and their counts. Therefore, we map the items to their respective counts using the
4. Using Counter class:
Counterclass is located in the
Counterclass is a built-in Python class that can be used to count the frequency of elements in a list or a sequence. You can simply pass the series to the
Counterclass and get the frequency counts.
5. Using "groupby()" method:
pandasis used to group rows of a DataFrame or Series object based on one or more columns or indexes.
- After grouping, we can apply various statistical functions or other operations on each group to aggregate or transform the data.
count()aggregate function is applied in the example below after grouping the data.