Get startedGet started for free

Counting made easy

1. Counting made easy

As a data scientist, we're often going to need to count items, create dictionaries values before we know keys to store them in, or structure complex types.

2. Collections Module

The collections module is part of Python standard library and holds several more advanced data containers which solve these problems and more. Let's start our tour of the collection module by learning about Counter.

3. Counter

Counter is a powerful python object based on the dictionary object that accepts a list and counts the number of times a value is found within the elements of that list. Since it's based on a dictionary, you can use all the normal dictionary features. Here, I have an list named nyc_eatery_types that contains one column of data called type from a table about eateries in NYC parks. I create a new Counter based on that list and print it. You can see each type from list and the number of times it was found in the list. I can also see how many restaurants are in the counter by using Restaurant as the index and printing it. Counters also provide a wonderful way to find the high values they contain.

4. Counter to find the most common

The most_common() method on a Counter returns a list of tuples containing the items and their count in descending order. Here I'm printing the top 3 eatery types in the NYC park system with the most_common method and passing it 3 as the number items to return. most_common() is great for frequency analytics, how often something occurs, a problem I encounter often when working on data science problems.

5. Let's practice!

Now it's your turn.