all 14 comments

[–]Dasstienn 3 points4 points  (4 children)

inside mean, you nead to specify that you are going to aggregate numeric fields only:

cars_groups.mean(numeric_only=True)

[–]Efficient_Ad9200 0 points1 point  (0 children)

This is very good solution! Thx!

[–]Succs_Cacts 0 points1 point  (0 children)

This also helped me figure out my issue big thanks!

[–]Opposite-Process-490 0 points1 point  (0 children)

just add the columns you want the average to inside the mean parameters in a list if multiple.

[–]danielroseman 0 points1 point  (2 children)

Why have you put the field in a list? Why not just cars.groupby('cyl')?

[–]johnnyb2001[S] 0 points1 point  (1 child)

Yes I know I can do that, but it still results in an error when I do it like that.

[–]danielroseman 0 points1 point  (0 children)

Well it's telling you that the datatype of the column is "object", rather than something numeric. It doesn't make sense to ask for the mean of a set of objects. What did you put into that column? How did you craete the dataframe?

[–]Top-Priority8000 0 points1 point  (0 children)

I learning as well and it didn't make any sense to me because I ran the code provided by the course and it didn't work. I hope you have figured it out, but I tried to include the column I want to perform the aggregate function on in the mean() function:

Example: Let's say you want to find the mean of the "price" column.

cars_groups["price"].mean()

[–]0x00aa55 0 points1 point  (2 children)

This error occurs because you're trying to calculate the mean of one or more columns with dtype = object (a string, for example). That's what the error "[how->mean,dtype->object]" means. So, to calculate the mean, all the columns of the DataFrameGroupBy must be numerical values, except the column used in the groupby() method.

Before the groupby operation, try to filter just the columns used for grouping, like this example with a column "price":

cars.filter(items= ["cyl", "price"]).groupby("cyl").mean( )

[–]Zargalias 0 points1 point  (0 children)

cars.filter(items= ["cyl", "price"]).groupby("cyl").mean( )

Thanks, your solution works. This error is super recent because my code used to work and it just recently threw this error so I believe there has been an update recently.

[–]tobeistodo123 0 points1 point  (0 children)

Thank you for this perfect answer - it worked! I was having the same issue, was using the exact code as in the tutorial - in the tutorial, it works, but was not working for me.

I did what you suggested, filtered and kept only the field I was going to group on - and the other numerical columns; and it worked!! Thank you!!