Skip to content
Advertisement

Pandas groupby – Find mean of first 10 items

I have 30 items in each group.

To find mean of entire items, I use this code.

JavaScript

That returns a value like this.

JavaScript

However, I would like to find the mean of the first 10 items in the group instead of the entire items.

JavaScript

That code return only a single Value instead of a pandas series.

So I’m getting errors like this.

AttributeError: 'numpy.float64' object has no attribute 'shift'

What is the proper way to get the pandas series instead of a single value?

Advertisement

Answer

You can try

JavaScript
JavaScript

In .groupby("Date").head(10).mean(), groupby.head() returns the DataFrame, .mean() is operated on the whole DataFrame rather than the group.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement