Member-only story
23 Efficient Ways of Subsetting a Pandas DataFrame
With Selection, Slicing, Indexing and Filtering
In part 1 and part 2, we’ve learned how to inspect, describe and summarize a Pandas DataFrame. Today, we’ll learn how to extract a subset of a Pandas DataFrame. This is very useful because we often want to perform operations on subsets of our data. There are many different ways of subsetting a Pandas DataFrame. You may need to select specific columns with all rows. Sometimes, you want to select specific rows with all columns or select rows and columns that meet a specific criterion, etc.
All different ways of subsetting can be divided into 4 categories: Selection, Slicing, Indexing and Filtering.
As you continue reading this post, you’ll learn the differences between these categories.
Before discussing any of the methods of subsetting a data frame, it is worth distinguishing between a Pandas Series object and a Pandas DataFrame object.
Pandas Series and DataFrame objects
The Series and the DataFrame are two main data structures in Pandas. Simply, a Series is…