Filtering

We saw how to get subsets of our data using row and column ranges, but how do we only take the data that meets some criteria? Pandas gives us a few options, including Boolean masks and some special methods. With Boolean masks, we test our data against some value and get a structure of the same shape back, except it is filled with True/False values; pandas can use this to select the appropriate values for us. For example, we can see which entries in the mag column had a magnitude greater than 2:

>>> df.mag > 20    False1    False2     True3    False4     True       ...Name: mag, dtype: bool

While we can run this on the entire dataframe, it wouldn't be too useful with our earthquake data since we have columns of various data types.

However, we can use ...

Get Hands-On Data Analysis with Pandas 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.