Preserving Series with the where method
Boolean indexing necessarily filters your dataset by removing all the rows that don't match the criteria. Instead of dropping all these values, it is possible to keep them using the where
method. The where
method preserves the size of your Series or DataFrame and either sets the values that don't meet the criteria for missing or replaces them with something else.
Getting ready
In this recipe, we pass the where
method boolean conditions to put a floor and ceiling on the minimum and maximum number of Facebook likes for actor 1 in the movie
dataset.
How to do it...
- Read the
movie
dataset, set the movie title as the index, and select all the values in theactor_1_facebook_likes
column that are not missing:
>>> movie = pd.read_csv('data/movie.csv', index_col='movie_title') >>> fb_likes = movie['actor_1_facebook_likes'].dropna() >>> fb_likes.head() movie_title Avatar 1000.0 Pirates of the Caribbean...