Chapter 11. Filtering and Sorting Rows

Whereas the previous chapter was about columns, this chapter is all about the rows in a DataFrame1. We’ll mainly look at two types of operations you can perform on rows:

  • Filtering rows using the df.filter() method

  • Sorting rows using the df.sort() method

With filtering, you select a subset of the rows, based on their values. With sorting, you reorder the rows based on their values; the number of rows remains the same. Besides that we’ll discuss various other methods that are related to filtering and sorting.

You’ll be working with a small DataFrame about power tools you’ll typically find in the garage of an amateur woodworker. For each tool, we have its type, product code, brand, whether it’s cordless or not, its price, and RPM (revolutions per minute). Here’s what the tools DataFrame looks like:

tools = pl.read_csv("data/tools.csv")
tools
shape: (10, 6) ┌───────────────────────┬──────────────┬────────┬──────────┬───────┬───────┐ ...

Get Python Polars: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.