Feb-29-2024, 06:13 PM
(This post was last modified: Feb-29-2024, 06:13 PM by deanhystad.)
Instead of this
mpgy = mpg.groupby("model_year").mean()["mpg"]You need this
mpgy = mpg.groupby("model_year")["mpg"].mean()The top one computes the mean for all the columns and selects the "mpg" column. That doesn't work when some columns are not numeric such as "name" in your file. The second one computes the mean of only the "mpg" column. You can see this in the example below were mean() is applied to mpg and weight.
import pandas as pd df = pd.read_csv("mpg.csv")[["model_year", "mpg", "weight"]] print(df.groupby("model_year").mean())
Output: mpg weight
model_year
70 17.689655 3372.793103
71 21.250000 2995.428571
72 18.714286 3237.714286
73 17.100000 3419.025000
74 22.703704 2877.925926
75 20.266667 3176.800000
76 21.573529 3078.735294
77 23.375000 2997.357143
78 24.061111 2861.805556
79 25.093103 3055.344828
80 33.696552 2436.655172
81 30.334483 2522.931034
82 31.709677 2453.548387