简体   繁体   English

pandas groupby last n

[英]pandas groupby last n

What is the best way to get the mean of the last n instances using pandas groupby? 使用pandas groupby获取最后n个实例的平均值的最佳方法是什么?

For example I have a dataframe like this: 例如,我有一个这样的数据帧:

frame = pd.DataFrame({'Student' : ['Bob', 'Bill', 'Bob', 'Bob', 'Bill', 'Joe', 'Joe', 'Bill', 'Bob', 'Joe'],                                      
                          'Score' : np.random.random(10)})

how do I get the mean of the last 3 scores for each student. 我如何得到每个学生最后3分的平均值。

Maybe something like this? 也许是这样的?

>>> df.groupby("Student")["Score"].apply(lambda x: x.iloc[-3:].mean())
Student
Bill       0.513128
Bob        0.342806
Joe        0.469662
Name: Score, dtype: float64

You can access the last three (or fewer) elements using .iloc[-3:] , and then take the mean using .mean() . 您可以使用.iloc[-3:]访问最后三个(或更少)元素,然后使用.mean()取平均值。

Alternatively, you could use .tail(3) instead, or do it in two passes: 或者,您可以改为使用.tail(3) ,或者两次传递:

>>> df.groupby("Student").tail(3).groupby("Student")["Score"].mean()
Student
Bill       0.513128
Bob        0.342806
Joe        0.469662
Name: Score, dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM