【问题标题】:Ranking and subranking rows in pandasPandas 中的行排序和子排序
【发布时间】:2019-01-23 11:16:36
【问题描述】:

我有以下数据集,我想按地区和商店类型(在每个地区内)对其进行排名。

有没有一种巧妙的方式在 python 中对这两列进行编码?

数据:

print (df)
    Region ID Location store Type ID  Brand share
0           1              Warehouse         1.97
1           1              Warehouse         0.24
2           1           Super Centre         0.21
3           1              Warehouse         0.13
4           1         Mini Warehouse         0.10
5           1           Super Centre         0.07
6           1         Mini Warehouse         0.04
7           1           Super Centre         0.02
8           1         Mini Warehouse         0.02
9          10              Warehouse         0.64
10         10         Mini Warehouse         0.18
11         10              Warehouse         0.13
12         10              Warehouse         0.09
13         10           Super Centre         0.07
14         10         Mini Warehouse         0.03
15         10         Mini Warehouse         0.02
16         10           Super Centre         0.02

【问题讨论】:

标签: python pandas rank


【解决方案1】:

使用GroupBy.cumcount:

df['RegionRank'] = df.groupby('Region ID')['Brand share'].cumcount() + 1
cols = ['Location store Type ID', 'Region ID']
df['StoreTypeRank'] = df.groupby(cols)['Brand share'].cumcount() + 1
print (df)
    Region ID Location store Type ID  Brand share  RegionRank  StoreTypeRank
0           1              Warehouse         1.97           1              1
1           1              Warehouse         0.24           2              2
2           1           Super Centre         0.21           3              1
3           1              Warehouse         0.13           4              3
4           1         Mini Warehouse         0.10           5              1
5           1           Super Centre         0.07           6              2
6           1         Mini Warehouse         0.04           7              2
7           1           Super Centre         0.02           8              3
8           1         Mini Warehouse         0.02           9              3
9          10              Warehouse         0.64           1              1
10         10         Mini Warehouse         0.18           2              1
11         10              Warehouse         0.13           3              2
12         10              Warehouse         0.09           4              3
13         10           Super Centre         0.07           5              1
14         10         Mini Warehouse         0.03           6              2
15         10         Mini Warehouse         0.02           7              3
16         10           Super Centre         0.02           8              2

GroupBy.rank,但它为相同的类别返回相同的值:

df['RegionRank'] = (df.groupby('Region ID')['Brand share']
                       .rank(method='dense', ascending=False)
                       .astype(int))
cols = ['Location store Type ID', 'Region ID']
df['StoreTypeRank'] = (df.groupby(cols)['Brand share']  
                           .rank(method='dense', ascending=False)
                           .astype(int))
print (df)
    Region ID Location store Type ID  Brand share  RegionRank  StoreTypeRank
0           1              Warehouse         1.97           1              1
1           1              Warehouse         0.24           2              2
2           1           Super Centre         0.21           3              1
3           1              Warehouse         0.13           4              3
4           1         Mini Warehouse         0.10           5              1
5           1           Super Centre         0.07           6              2
6           1         Mini Warehouse         0.04           7              2
7           1           Super Centre         0.02           8              3
8           1         Mini Warehouse         0.02           8              3
9          10              Warehouse         0.64           1              1
10         10         Mini Warehouse         0.18           2              1
11         10              Warehouse         0.13           3              2
12         10              Warehouse         0.09           4              3
13         10           Super Centre         0.07           5              1
14         10         Mini Warehouse         0.03           6              2
15         10         Mini Warehouse         0.02           7              3 <-same value .02 
16         10           Super Centre         0.02           7              2 <-same value .02 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2017-03-20
    相关资源
    最近更新 更多