I have 30 items in each group.
To find mean of entire items, I use this code.
JavaScript
x
2
1
y = df[["Value", "Date"]].groupby("Date").mean()
2
That returns a value like this.
JavaScript
1
8
1
Date Value
2
3
2020-01-01 00:30:00 7172.36
4
2020-01-01 01:00:00 7171.55
5
2020-01-01 01:30:00 7205.90
6
2020-01-01 02:00:00 7210.24
7
2020-01-01 02:30:00 7221.50
8
However, I would like to find the mean of the first 10 items in the group instead of the entire items.
JavaScript
1
2
1
y1 = df[["Value", "Date"]].groupby("Date").head(10).mean()
2
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
1
2
1
y1 = df[["Value", "Date"]].groupby("Date").apply(lambda g: g['Value'].head(10).mean())
2
JavaScript
1
10
10
1
print(y1)
2
3
Date
4
2020-01-01 00:30:00 7172.36
5
2020-01-01 01:00:00 7171.55
6
2020-01-01 01:30:00 7205.90
7
2020-01-01 02:00:00 7210.24
8
2020-01-01 02:30:00 7221.50
9
dtype: float64
10
In .groupby("Date").head(10).mean()
, groupby.head()
returns the DataFrame, .mean()
is operated on the whole DataFrame rather than the group.