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: SelectionSlicing, **Indexing **and Filtering.

(Image by author)

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.

#programming #python #pandas #efficient ways of subsetting a pandas dataframe #pandas dataframe #subset

23 Efficient Ways of Subsetting a Pandas DataFrame
1.75 GEEK