Filtering

The filter method enables us to apply filtering to a groupby object to result in a subset of the initial object.

Let's apply filter to the sample sales data to compute only the sums of those groups whose length is more than 10000, when grouped across Category:

filtered_df = sales_data[["Category", "Quantity"]].set_index("Category").groupby("Category").filter(lambda x: len(x) > 10000)
filtered_df.groupby("Category").sum()

The following will be the output:

 Filtering with groupby

Now, as you can see, filtering removes the Furniture category, whose length is less than 10000.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset