In chapter 4, we learned how to extract rows, columns, and cell values from a DataFrame by using the loc and iloc accessors. These accessors work well when we know the index labels and positions of the rows/columns we want to target. Sometimes, we may want to target rows not by an identifier but by a condition or a criterion. We may want to extract a subset of rows in which a column holds a specific value, for example.
In this chapter, we’ll learn how to declare logical conditions that include and exclude rows from a DataFrame. We’ll see how to combine multiple conditions by using AND and OR logic. Finally, we’ll introduce some pandas utility methods that simplify the filtering process. Lots of fun lies ahead, so let’s jump in.