Recall & Review
beginner
What does the
filter() function do in pandas groupby objects?It keeps or removes entire groups based on a condition applied to each group. It helps to select groups that meet a specific rule.
Click to reveal answer
beginner
How do you use
filter() to keep groups with more than 3 rows?Use
df.groupby('column').filter(lambda x: len(x) > 3). This keeps only groups where the number of rows is greater than 3.Click to reveal answer
beginner
True or False:
filter() changes the original DataFrame structure by removing some rows.True. It returns a new DataFrame with only the rows from groups that passed the filter condition.
Click to reveal answer
beginner
What kind of function do you pass to
filter()?You pass a function that takes a group (DataFrame) and returns True or False. True keeps the group, False removes it.
Click to reveal answer
beginner
Give an example use case for
filter() in group-level filtering.For example, keep only customers who made more than 5 purchases. Group by customer, then filter groups with size > 5.
Click to reveal answer
What does
filter() do when used after groupby() in pandas?✗ Incorrect
filter() works on groups and keeps or removes whole groups based on the condition you give.
Which of these is a valid condition function for
filter()?✗ Incorrect
The function must return True or False. Checking if mean > 10 returns a boolean.
If you want to keep groups with at least 5 rows, what is the correct filter condition?
✗ Incorrect
Length of group >= 5 keeps groups with 5 or more rows.
What is the output type of
groupby().filter()?✗ Incorrect
The output is a DataFrame containing only rows from groups that passed the filter.
Can
filter() be used to filter groups based on aggregated statistics like sum or mean?✗ Incorrect
You can use any condition that returns True or False, including those based on aggregated values.
Explain how to use
filter() to keep only groups where the average of a column is above a threshold.Think about passing a function that calculates mean and returns True or False.
You got /5 concepts.
Describe the difference between
filter() and apply() when used with groupby in pandas.Focus on what each function returns and how they treat groups.
You got /4 concepts.