O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filtering groups from aggregation

Groups of data can be selectively dropped from processing using .filter(). This method is supplied a function that can be used to make group-level decisions on whether the entire group is included in the result after the combination. The function should return True if the group is to be included in the result and False to exclude it.

We will examine several scenarios using the following data:

The first demonstration will drop groups that do not have a minimum number of items. Specifically, they will be dropped if they only have one item or less:

The following example will omit groups that have any NaN values: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required