Slicing a DataFrame

Having discussed slicing a series, let's talk about slicing a DataFrame. The good news is that, in talking about series slicing, a lot of the hard work is already done. We introduced loc and iloc as series methods, but they are DataFrame methods as well. After all, you should be thinking of DataFrames as multiple series glued together as columns.

We now need to think about how what we learned for series translates to a two-dimensional setting. If we use bracket notation, it will work only for the columns of the DataFrame. We will need to use loc and iloc to subset rows of the DataFrame. In truth, these methods can accept two positional arguments. The first positional argument determines which rows to select, and the second ...

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