Pandas 如何使用groupby从某一列中获取第n个最大值,并对同一行的另一列进行计算

Pandas 如何使用groupby从某一列中获取第n个最大值,并对同一行的另一列进行计算,pandas,pandas-groupby,Pandas,Pandas Groupby,我有一个熊猫数据帧dd: Experiment Position Lap ID Partition Value Expt1 2000 104 127327203 2 52.01 Expt1 2000 105 127327203 2 51.78 Expt1 2000 106 127327203 2 51.57 Expt1 2000

我有一个熊猫数据帧dd:

Experiment  Position    Lap ID     Partition    Value
Expt1       2000        104 127327203   2       52.01
Expt1       2000        105 127327203   2       51.78
Expt1       2000        106 127327203   2       51.57
Expt1       2000        107 127327203   2       51.63
Expt1       2000        108 127327203   2       51.61
Expt1       2000        109 127327203   2       51.78
Expt1       2000        110 127327203   2       51.78
Expt1       2000        111 127327203   2       51.53
Expt1       2000        112 127327203   2       51.69
Expt1       2000        113 127327203   2       51.53
Expt1       2000        114 127327203   2       51.40
Expt1       2000        115 127327203   2       51.45
Expt1       2000        116 127327203   2       51.47
Expt1       2000        117 127327203   2       51.61
Expt1       2000        118 127327203   2       50.89
Expt1       2500        104 127327203   2       52.16
Expt1       2500        105 127327203   2       53.14
Expt1       2500        106 127327203   2       52.02
我的数据是几千行,有很多实验,所以上面只是一个快照

我想分组实验,然后定位,然后圈

grouped = dd.groupby(['Experiment','Position','Lap']) 
grouped.first()
这给了我:

现在我只想使用“Lap”列中的第十大值来给出“Value”列的平均值和标准值

如果可能的话,我想输出到一个新的数据框,实验,位置和上述计算的结果,这样我就可以绘图了

感谢您提供的任何帮助

第一次按计数进行筛选,按和,对于
>=10
,以及:

由具有以下内容的多个列使用:


这是完美的,我不知道你们可以用尾巴的方式过滤出小于10组大小。我如何获得第5百分位而不是std?再次感谢,这真是太棒了。我刚刚意识到,对于2500的位置值,其中只有3个值,我不希望这些值有助于计算-只有在有10个或更多值的情况下。@user11305439-编辑的答案。这太棒了…但理解起来相当复杂。你能为这些例子推荐一些阅读材料吗。我在网上没有找到任何东西。谢谢again@user11305439-这个问题很难回答,也许有点帮助,尤其是现代熊猫。
df = df[df.groupby(['Experiment','Position'])['Value'].transform('size').ge(10)]
df1 = (df.sort_values(['Experiment','Position','Lap', 'Value'])
        .groupby(['Experiment','Position'])
        .tail(10))
print (df1)
   Experiment  Position  Lap         ID  Partition  Value
5       Expt1      2000  109  127327203          2  51.78
6       Expt1      2000  110  127327203          2  51.78
7       Expt1      2000  111  127327203          2  51.53
8       Expt1      2000  112  127327203          2  51.69
9       Expt1      2000  113  127327203          2  51.53
10      Expt1      2000  114  127327203          2  51.40
11      Expt1      2000  115  127327203          2  51.45
12      Expt1      2000  116  127327203          2  51.47
13      Expt1      2000  117  127327203          2  51.61
14      Expt1      2000  118  127327203          2  50.89

df2 = df1.groupby(['Experiment','Position'])['Value'].agg([('avg','mean'),
                                                           ('q5', lambda x: x.quantile(.5))])
print (df2)
                        avg     q5
Experiment Position               
Expt1      2000      51.513  51.53