Selecting with booleans, integer location, and labels
Chapter 10, Selecting Subsets of Data, covered a wide range of recipes on selecting different subsets of data through the .iloc
and .loc
indexers. Both these indexers select rows and columns simultaneously by either integer location or label. Both these indexers can also do data selection through boolean indexing, even though booleans are not integers and not labels.
Getting ready
In this recipe, we will filter both rows and columns with boolean indexing for both the .iloc
and .loc
indexers.
How to do it...
- Read in the movie dataset, set the index as the title, and then create a boolean Series matching all movies with a content rating of
G
and an IMDB score less than4
:
>>> movie = pd.read_csv('data/movie.csv', index_col='movie_title') >>> c1 = movie['content_rating'] == 'G' >>> c2 = movie['imdb_score'] < 4 >>> criteria = c1 & c2
- Let's first pass these criteria to the
.loc
indexer to filter the rows...