分组数据中的排名值

时间:2019-12-23 18:41:08

标签: python pandas

我正在寻找一种有效的方法来重新编号Python中分组数据帧的秩向量。

首先,我在简单数据中根据soldier_typecounts进行排名。到目前为止,这很简单!太太,我需要根据以下条件对士兵进行重新排名:

如果soldier_type == S1regimenttrucks1的每一组中,我都希望它始终排在2的位置,然后从排名counts(从最高import pandas as pd from numpy.random import seed from numpy.random import randint seed(1234) raw_data = {'regiment': ['51st', '51st', '51st', '51st', '51st', '51st', '51st', '51st', '51st', '51st', '51st', '51st'], 'trucks': ['MAZ-7310', 'MAZ-7310', 'MAZ-7310', 'MAZ-7310', 'Tatra 810', 'Tatra 810', 'Tatra 810', 'Tatra 810', 'ZIS-150', 'ZIS-150', 'ZIS-150', 'ZIS-150'], 'soldier_type': ['S1', 'S2', 'S3', 'S4', 'S1', 'S3', 'S4', 'S5', 'S1', 'S2', 'S4', 'S5'], 'counts': randint(1,100,12)} df = pd.DataFrame(raw_data, columns = ['regiment', 'trucks','soldier_type', 'counts']) regiment trucks soldier_type counts 0 51st MAZ-7310 S1 48 1 51st MAZ-7310 S2 84 2 51st MAZ-7310 S3 39 3 51st MAZ-7310 S4 54 4 51st Tatra 810 S1 77 5 51st Tatra 810 S3 25 6 51st Tatra 810 S4 16 7 51st Tatra 810 S5 50 8 51st ZIS-150 S1 24 9 51st ZIS-150 S2 27 10 51st ZIS-150 S4 31 11 51st ZIS-150 S5 44 def rank_soldier_type (df): df = df.assign(rank_ = df.groupby(['regiment','trucks'])['counts'].rank(ascending = False,method='dense')) return df #1st part #%% if df.soldier_type != 'S1' and df.rank_ == 1 : df['new_rank_'] = 1 else: df['new_rank_'] = df['rank_'].rank(ascending = False,method='dense') return df df = rank_soldier_type(df) 到最低)。

这是我解决这个问题的尝试:

rank_

如果我运行此功能的第一部分,则可以创建df = rank_soldier_type(df) regiment trucks soldier_type counts rank_ 0 51st MAZ-7310 S1 48 3.0 1 51st MAZ-7310 S2 84 1.0 2 51st MAZ-7310 S3 39 4.0 3 51st MAZ-7310 S4 54 2.0 4 51st Tatra 810 S1 77 1.0 5 51st Tatra 810 S3 25 3.0 6 51st Tatra 810 S4 16 4.0 7 51st Tatra 810 S5 50 2.0 8 51st ZIS-150 S1 24 4.0 9 51st ZIS-150 S2 27 3.0 10 51st ZIS-150 S4 31 2.0 11 51st ZIS-150 S5 44 1.0 列:

        regiment     trucks soldier_type  counts  rank_  new_rank_
    0      51st   MAZ-7310           S1      48    3.0       1.0
    1      51st   MAZ-7310           S2      84    1.0       2.0
    2      51st   MAZ-7310           S3      39    4.0       4.0
    3      51st   MAZ-7310           S4      54    2.0       3.0
    4      51st  Tatra 810           S1      77    1.0       1.0
    5      51st  Tatra 810           S3      25    3.0       3.0
    6      51st  Tatra 810           S4      16    4.0       4.0
    7      51st  Tatra 810           S5      50    2.0       2.0
    8      51st    ZIS-150           S1      24    4.0       1.0
    9      51st    ZIS-150           S2      27    3.0       4.0 
    10     51st    ZIS-150           S4      31    2.0       3.0
    11     51st    ZIS-150           S5      44    1.0       2.0

预期输出;

callback(null,arg2)

1 个答案:

答案 0 :(得分:1)

通过添加duplicated

修改代码
df['New']=df[df[['regiment', 'trucks']].duplicated()].\
            groupby(['regiment', 'trucks'])['counts'].rank(ascending=False, method='dense')+1
df.New.fillna(1,inplace=True)
df
Out[35]: 
   regiment     trucks soldier_type  counts  New
0      51st   MAZ-7310           S1      48  1.0
1      51st   MAZ-7310           S2      84  2.0
2      51st   MAZ-7310           S3      39  4.0
3      51st   MAZ-7310           S4      54  3.0
4      51st  Tatra 810           S1      77  1.0
5      51st  Tatra 810           S3      25  3.0
6      51st  Tatra 810           S4      16  4.0
7      51st  Tatra 810           S5      50  2.0
8      51st    ZIS-150           S1      24  1.0
9      51st    ZIS-150           S2      27  4.0
10     51st    ZIS-150           S4      31  3.0
11     51st    ZIS-150           S5      44  2.0