Skip to content
Advertisement

pandas: filter rows of DataFrame with operator chaining

Most operations in pandas can be accomplished with operator chaining (groupby, aggregate, apply, etc), but the only way I’ve found to filter rows is via normal bracket indexing

JavaScript

This is unappealing as it requires I assign df to a variable before being able to filter on its values. Is there something more like the following?

JavaScript

Advertisement

Answer

I’m not entirely sure what you want, and your last line of code does not help either, but anyway:

“Chained” filtering is done by “chaining” the criteria in the boolean index.

JavaScript

If you want to chain methods, you can add your own mask method and use that one.

JavaScript
Advertisement