The chapter introduces pandas performance as a practical concern: modern computers are powerful, but large data sets can still feel slow and memory-hungry. The central idea is that pandas works best when you keep data slim, stay as much as possible in NumPy-like structures, and measure both memory use and execution time before optimizing. It emphasizes that performance work is often about using only the columns you need, choosing efficient data types, and understanding what pandas is doing behind the scenes.
A major focus is memory reduction through categorical data. The text shows how to inspect memory with memory_usage and why a deep check is necessary to count Python string objects accurately. It then demonstrates converting repeated text columns into categories, which stores each distinct string once and replaces repeated values with compact internal codes. Using this approach on the Olympics data dramatically cuts memory usage, and the chapter explains how to identify the best candidate columns by looking for repeated values, as well as how to define categories ahead of time when future values are expected.
The chapter also explores faster reading and writing with Apache Arrow and its feather format, showing that binary formats can be much smaller and quicker than CSV or JSON for both saving and loading. It then shifts to query performance, comparing traditional boolean indexing with query, eval, and related tools. The takeaway is that query and eval can reduce temporary objects and speed up complex expressions, especially on large data sets, while isin is often the best choice for membership tests. Overall, the chapter encourages careful benchmarking and choosing the right tool for the data size, query complexity, and storage format involved.
Summary
Calculations and analysis with pandas are much faster than they would be in pure Python. Even so, when you’re working with a large data set, you’ll often want or need to reduce the memory footprint of your data frame and use techniques that can improve performance. In this chapter, we reviewed a number of techniques you can use to speed up your queries and also use fewer resources:
- Choosing columns carefully
- Reducing memory usage with categories
- Reading data from feather format rather than CSV
- Speeding up complex queries with df.query and df.eval
If you’ve reached this part of the book, congratulations! You’ve successfully gone through all 50 exercises! I have no doubt that if you’ve made it this far, you have a much better, deeper understanding of pandas, what it can do, and how you can use it in a variety of situations. You should feel good about yourself and confident about your ability to use pandas at work.
But before you stop reading: the next chapter contains a large project in which I’ll ask you to use all the techniques from this book to analyze a large real-world data set. I hope you’ll take the time to do the project, which will help cement the lessons you’ve learned and help you use pandas even more effectively in the future.
Pandas Workout ebook for free