...

/

Solution Review: Group By Aggregations

Solution Review: Group By Aggregations

This lesson provides the solution to the previous challenge.

We'll cover the following...

Group by aggregations #

Press + to interact
import pandas as pd
# Loading dataset
def read_csv():
# Define the column names as a list
names = ["mpg", "cylinders", "displacement", "horsepower", "weight", "acceleration", "model_year", "origin", "car_name"]
# Read in the CSV file from the webpage using the defined column names
df = pd.read_csv("auto-mpg.data", header=None, names=names, delim_whitespace=True)
return df
# Describing data
def group_aggregation(df, group_var, agg_var):
# Grouping the data and taking mean
grouped_df = df.groupby([group_var])[agg_var].mean()
return grouped_df
# Calling the function
print(group_aggregation(read_csv(),"cylinders","mpg"))

According to the problem statement, we need to group the Auto MPG Dataset on the basis of a column. Then we have to calculate the mean of the grouped data according to another column. Before doing it, we have to read the data first. There is no need to explain how to read the data, as we studied that in detail previously. Dataset is read from line 4 to line 9. ...