Solution Review: Group By Aggregations
This lesson provides the solution to the previous challenge.
We'll cover the following...
Group by aggregations
Python 3.5
import pandas as pd# Loading datasetdef read_csv():# Define the column names as a listnames = ["mpg", "cylinders", "displacement", "horsepower","weight", "acceleration", "model_year", "origin", "car_name"]# Read in the CSV file using regex for whitespace separationdf = pd.read_csv("auto-mpg.data", header=None, names=names, sep=r"\s+")return df# Describing datadef group_aggregation(df, group_var, agg_var):# Grouping the data and taking meangrouped_df = df.groupby([group_var])[agg_var].mean()return grouped_df# Calling the functionprint(group_aggregation(read_csv(), "cylinders", "mpg"))
According to the problem statement, we need to group the Auto MPG Dataset on the basis of ...