Home > front end >  How to show mean MPG and HP by manufacturer only in mtcars python dataset?
How to show mean MPG and HP by manufacturer only in mtcars python dataset?

Time:03-25

im very new to using panda, and im trying to figure out how to do this problem. summarize: show the mean mpg and hp for each manufacturer. I have this code:

mtcars = pd.read_csv('mtcars.csv')

list(mtcars)
mtcars.describe()

mtcars.rename(columns={"Unnamed: 0": "Make n Model"}, inplace=True)

list(mtcars)
print(mtcars.groupby("Make n Model")[["mpg", "hp"]].mean())

Which makes a list, but I dont understand how I would sort by manufacturer only. Right now it just lists the MPG and HP for each car model. Any help is appreciated, I've been at this for hours

CodePudding user response:

I'll consider that the "Make n Model" is the manufacturer column in your dataframe. So to groupby manufacturer and take the means and output a newer dataframe with manufacturers as index you would have to do

import pandas as pd

df = pd.read_csv(...)

df_grouped = df.groupby("Make n Model")[["mpg", "hp"]].mean()

If your goal is to have a column in the first dataframe with the mean mpg or hp by manufacturer you could do:

df["mean_mpg_manuf"]=df.groupby("Make n Model")["mpg"].transform("mean")

You could do the same for hp, the idea is that the .transform outputs a pandas series with the same size of the original dataframe with the values mapped to the manufacturer of each line

CodePudding user response:

Use a regular expression to get the manufacturer from the make and model.

mtcars['Make'] = mtcars['Make n Model'].str.extract(r'^(\w )')

Then you can use .groupby('Make')

  • Related