More Aggregations

Explore the `.transform` and the `.filter` methods of a groupby object.

Aggregations while keeping rows

Let’s assume we’re still looking at the JetBrains dataset, and we want to add a new column, the count of responses from a country. One way to do that would be to create a pivot table (or groupby) of the count of responses for each country and then merge that data back into the original DataFrame. However, if we use the transform method following groupby, we get the aggregation, but they’re not collapsed. The result is in terms of the original index.

So, one of the reasons we should gravitate toward groupby instead of pivot_table is the flexibility it offers.

Here’s the count of the country for each original row. We can provide our own function to the transform method or take advantage of existing functions. We want to use the 'size' function to get new counts. However, we just want to apply it to a single column, and it doesn’t matter which column we choose, so we’ll use age:

Get hands-on with 1200+ tech skills courses.