When working with data, we are often going to need to count items, create dictionaries values before we know keys to store them in, or maintain order in a dictionary.
Counter is a powerful tool for counting, validating, & learning more about the elements within a dataset that is found in the
collections module. We pass an iterable (list, set, tuple) or a dictionary to the
Counter. We can also use the
Counter object similarly to a dictionary with key/value assignment, for example,
counter[key] = value.
A common usage for
Counter is checking data for consistency prior to using it.
counter module is based on dictionary; we can use all of the normal dictionary features. In this example, we have the list named
nyc_eatery_types that contains one column of data called type from a table about eateries in NYC parks. We create a new counter based on that list and print it.
We can see each type from the list & the number of times it was found in the list. We can also see how many restaurants are in the counter by using
Restaurant as the index and printing it.
Using Counter to Find the Most Common
Counters also provide a wonderful way to find the most common values they contain. The
most_common() method on a Counter returns a list of tuples containing the items & their count in descending order.
Let’s print the top 3 eatery types in the NYC park system with the
most_common() method & pass it 3 as the number items to return.
most_common() is excellent for frequency analytics & finding out how often an item occurs.
In the following example, we will:
- Use the data from the Chicago Transit Authority on ridership.
- Import the
- Print the first ten items from the
- Create a
- Print the
When we run the above code, it produces the following result: