百分比在群体中的大熊猫排名

itj*_*s18 8 python statistics numpy scipy pandas

我无法弄清楚如何编写函数来完成分组百分位数.1985年至2012年,我拥有数据框架中的所有团队; 前10个显示如下:它目前按年份排序.我想给LgRnk分组的百分位数Year.因此,例如,1985年的23个LgRank(最差的球队)将是100个百分点,而1985年的1个LgRank(最佳球队)将是1个百分点.2010年的30 LgRank(最差的团队)将是百分之百等.它需要按年份不同数量的b/c进行分组LgRnk.

    Team                WLPer   Year LgRnk   W  L
19  Sacramento Kings    0.378   1985    18  31  51
0   Atlanta Hawks       0.415   1985    17  34  48
17  Phoenix Suns        0.439   1985    16  36  46
4   Cleveland Cavaliers 0.439   1985    15  36  46
13  Milwaukee Bucks     0.720   1985    3   59  23
3   Chicago Bulls       0.463   1985    14  38  44
16  Philadelphia 76ers  0.707   1985    4   58  24
22  Washington Wizards  0.488   1985    13  40  42
20  San Antonio Spurs   0.500   1985    12  41  41
21  Utah Jazz           0.500   1985    11  41  41
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下方法创建一个函数:scipy.stats.percentileofscore 我无法理解它.

And*_*den 10

您可以在LgRnk列上进行申请:

# just for me to normalize this, so my numbers will go from 0 to 1 in this example
In [11]: df['LgRnk'] = g.LgRnk.rank()

In [12]: g = df.groupby('Year')

In [13]: g.LgRnk.apply(lambda x: x / len(x))
Out[13]:
19    1.0
0     0.9
17    0.8
4     0.7
13    0.1
3     0.6
16    0.2
22    0.5
20    0.4
21    0.3
Name: 1985, dtype: float64
Run Code Online (Sandbox Code Playgroud)

系列groupby rank(刚刚适用Series.rank)采用pct参数来做到这一点:

In [21]: g.LgRnk.rank(pct=True)
Out[21]:
19    1.0
0     0.9
17    0.8
4     0.7
13    0.1
3     0.6
16    0.2
22    0.5
20    0.4
21    0.3
Name: 1985, dtype: float64
Run Code Online (Sandbox Code Playgroud)

并直接在WLPer列上(虽然由于绘制略有不同):

In [22]: g.WLPer.rank(pct=True, ascending=False)
Out[22]:
19    1.00
0     0.90
17    0.75
4     0.75
13    0.10
3     0.60
16    0.20
22    0.50
20    0.35
21    0.35
Name: 1985, dtype: float64
Run Code Online (Sandbox Code Playgroud)

注意:我已经更改了第一行的数字,因此您将在整个框架上获得不同的分数.