Preserving Series size with the .where method
When you filter with Boolean arrays, the resulting Series or DataFrame is typically smaller. The .where
method preserves the size of your Series or DataFrame and either sets the values that don't meet the criteria to missing or replaces them with something else. Instead of dropping all these values, it is possible to keep them.
When you combine this functionality with the other
parameter, you can create functionality similar to coalesce found in databases.
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 the
actor_1_facebook_likes
column that are not missing:>>> movie = pd.read_csv( ... "data/movie.csv", index_col="movie_title" ... ...