Renaming grouped aggregation columns

Don't you think the output dataframe would be more informative if we could rename the column name with the operation we performed in that column or group?

We can perform aggregation in each group and rename the columns according to the operation performed. This is useful for understanding the output dataset:

df.groupby(
["body-style","drive-wheels"]).agg(
# Get max of the price column for each group
max_price=('price', max),
# Get min of the price column for each group
min_price=('price', min),
# Get sum of the price column for each group
total_price=('price', 'mean')
)

The output of the preceding code is as follows:

As shown in the preceding screenshot, we only selected two categories: body-style and drive-wheels. For each row in these categories, the maximum price, the minimum price, and the total price is computed in the successive columns. 

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

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