Python's Counter class from the Collections module automatically counts occurrences in a list. The speaker shows how to create a Counter from NYC eatery types, access individual counts, and use most_common(n) to find the top three most frequent items.
Chapters
As a data scientist, we're often going to need to count items, create dictionary values before we know the keys to store them in or maintain order in a dictionary. The Collections module is part of the Python Standard library and holds several more advanced data containers which solve these problems and more. Let's start our tour of the Collections module by learning about 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 dictionary based, you can use all the normal dictionary features. Here I have a list named nycetery Tops that contains one column of data called Type from a table about eateries in the New York City parts. I create a new counter based on that list and print it. You can see each top from the list and the number of times it was found in the original list. I can also see how many restaurants there are in the counter by using restaurant as the index and printing it. 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 and their count in descending order. Here I'm printing the top three eatery types in the New York City park system with the most common method and passing it 3 as the number of items to return most common is great for frequency analytics and how often something occurs. A problem I encounter often when working on data science problems. Now it's your turn.