Learn Data Science from Data School 📊

Tuesday Tip #16: Smarter counting in Python 🤓

Published 10 months ago • 2 min read

Hi Reader!

My goal with Tuesday Tips is to help you get better at data science every week.

  1. Have the tips been helpful to you?
  2. What would make the tips even more helpful for you?

I look forward to hearing from you! 💬

Find past tips at (Yes, that’s a real URL!)

👉 Tip #16: Use Python’s Counter class

When I’m working with data in Python, I tend to use either built-in types (like lists and dictionaries) or third-party types (like NumPy arrays and pandas DataFrames), depending on the size and complexity of the data.

But occasionally, there are scenarios in which I use a more specialized type, such as “Counter” from the collections module. It solves a specific problem so well that creating my own solution would be wasted effort!

For example, let’s say that I need to count how many times a specific number appears in a dataset. Counter is the perfect tool for solving such a problem!

To start, we would import the Counter class from the collections module (which is part of Python’s standard library):

The basic usage of Counter is that you pass it an “iterable” and it counts the number of times each element appears.

Side note: An “iterable” is anything you can iterate through. If you can loop through an object using a “for loop”, you know it’s an iterable. Examples of iterables include strings, lists, tuples, dictionaries, and files.

For example, let’s pass the string ‘HELLO’ to Counter:

We get back a Counter object, which tells us that the ‘L’ appeared twice and the other letters each appeared once.

Let’s try creating a Counter object from a list of integers:

It counted how many times each integer appears in the list.

Let’s save it and check the type:

This confirms that c has the type Counter (or more formally, “c is an instance of the Counter class”). However, it looks very similar to a dictionary since it’s actually a “subclass” of dict.

As such, it acts like a dictionary, meaning when you pass it a key, it returns a value:

This tells us that the integer 7 appeared 4 times.

If you want the Counter to count more things, you pass another iterable to its update() method:

7 has now appeared 5 times, and 6 has appeared 2 times.

If you want to list the elements from most common to least common, you use the most_common() method, which returns a list of tuples:

7 is the most common, followed by 1, then 6, then 3.

Finally, you can pass this list to the sorted() function to sort it by the first element in each tuple, which is how we might prefer to see the results:

To see more examples and use cases, I recommend checking out the Counter class in Python’s official documentation.

If you enjoyed this week’s tip, please forward it to a friend! Takes only a few seconds, and it truly helps me out! 🙌

See you next Tuesday!

- Kevin

P.S. This is your Machine Learning system?

Did someone awesome forward you this email? Sign up here to receive data science tips every week!

Learn Data Science from Data School 📊

Kevin Markham

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School 📊

Hi Reader, Do any of these sound like you? You're new to the pandas library and you want to learn the fundamentals You have some experience with pandas, but you want to fill in the gaps in your knowledge You want to learn the best practices for data analysis with pandas in 2024 If so, you should enroll in my FREE course (launching today!), pandas in 30 days. Why learn pandas? pandas is a powerful, open source Python library for data analysis, manipulation, and visualization. If you're working...

9 days ago • 1 min read

Hi Reader, There's a gift for you somewhere in this email... just look for the 🎁 emoji! Tip #39: Six quick Python tricks Here's what I'll cover below: Return the number of unique values Count values with Counter Better debugging with f-strings Return multiple values from a function Count while looping Create a dictionary with a comprehension Let's get started! 👇 1️⃣ Return the number of unique values Need to know the number of unique values in an iterable? Convert it to a set and check the...

16 days ago • 2 min read

Hi Reader, My goal with Tuesday Tips is to help you get better at Data Science every week. Is there anything that would make these tips even more helpful for you? Let me know! 💬 You can find past tips at (Yes, that’s a real URL!) Tip #38: Five ways to rename your DataFrame columns Let's say that we have a simple pandas DataFrame: I prefer to use dot notation to select pandas columns, but that won't work since the column names have spaces. Let's fix this! The most flexible method...

23 days ago • 1 min read
Share this post