Selecting with Booleans, integer location, and labels
Previously, we covered a wide range of recipes on selecting different subsets of data through the .iloc
and .loc
attributes. Both of these select rows and columns simultaneously by either integer location or label.
In this recipe, we will filter both rows and columns with the .iloc
and .loc
attributes.
How to do it…
- Read in the movie dataset, set the index as the title, and then create a Boolean array matching all movies with a content rating of
G
and an IMDB score less than 4:>>> 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
.loc
to filter the rows:>>> movie_loc = movie.loc[criteria] >>> movie_loc...