Python Forum
DF.groupby(col).min works, mean gets a "not implemented" error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DF.groupby(col).min works, mean gets a "not implemented" error
#4
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
samgardner5 likes this post
Reply


Messages In This Thread
RE: DF.groupby(col).min works, mean gets a "not implemented" error - by deanhystad - Feb-29-2024, 06:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,457 Jun-18-2022, 01:09 PM
Last Post: snippsat
Brick Have I implemented this function correctly? naggafin 4 2,531 May-22-2022, 02:52 AM
Last Post: stevendaprano
  NotImplementedError: pseudo-class is not implemented - how to Update Python to solve apollo 1 3,227 May-16-2021, 08:03 AM
Last Post: buran
  Function throws error but then works? Milfredo 10 4,036 Sep-12-2020, 05:16 AM
Last Post: Milfredo
  delete a file works but with error Leon79 4 3,088 Jul-14-2020, 06:51 AM
Last Post: snippsat
  Could I override a fully implemented method zatlas1 2 2,503 Jun-06-2019, 02:20 AM
Last Post: zatlas1
  Script works ok on windows but gives error on ubuntu papampi 3 4,167 Oct-11-2017, 04:17 PM
Last Post: papampi
  How to you find the file where a class or a function was implemented? MisterX 4 4,310 Mar-16-2017, 09:51 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020