在熊猫多索引上添加自动增量列

卡伦

我下面有以下多索引熊猫。我正在尝试创建:

  1. 从“ project_rank”派生的自动增量计数器
  2. “ lob”中的最大项目数

但是我不确定我将如何做到这一点。任何指针都会有所帮助

之前的原始df_matrix:

print(df_matrix.head(10))
              lob          project_rank  duration_in_status
0     Commodities                CM LOB                 2.0
1     Commodities  Index Book Migration                25.0
2  Cross Platform                CM LOB                 0.0
3  Cross Platform                 CSAVA                16.0
4  Cross Platform     Calypso Migration                 0.0
5  Cross Platform       EMD / Delta One                 0.0
6  Cross Platform                  FRTB                68.0
7  Cross Platform  Index Book Migration                 1.0
8  Cross Platform           Instruments                 3.0
9  Cross Platform                 KOJAK                 0.0

之前的多索引:

                                         duration_in_status
lob            project_rank                            
Commodities    CM LOB                               2.0
               Index Book Migration                25.0
Cross Platform CM LOB                               0.0
               CSAVA                               16.0
               Calypso Migration                    0.0
               EMD / Delta One                      0.0
               FRTB                                68.0
               Index Book Migration                 1.0
               Instruments                          3.0
               KOJAK                                0.0
               LOB BOW                            324.0
               Non-Trading                          0.0
               Notes Workflow                      23.0
               PROD                                 0.0
               Result Service                      53.0
               Tech Debt                           96.0
Interest Rates LOB BOW                              0.0
Other          Notes Workflow                       0.0
Treasury       2B2                                  1.0

验收标准结果:

在此处输入图片说明

拉斐尔克

好像你想要的

df['proj_num'] = df.groupby('lob').project_rank.cumcount() + 1
df['depth'] = df.groupby('lob').project_rank.transform(len)

在应用多索引之前:)

    lob              project_rank       duration_in_status  proj_num    depth
0   Commodities      CMLOB               2.0                1           2
1   Commodities      IndexBookMigration  25.0               2           2
2   Cross_Platform   CMLOB               0.0                1           8
3   Cross_Platform   CSAVA               16.0               2           8
4   Cross_Platform   CalypsoMigration    0.0                3           8
5   Cross_Platform   EMD/DeltaOne        0.0                4           8
6   Cross_Platform   FRTB                68.0               5           8
7   Cross_Platform   IndexBookMigration  1.0                6           8
8   Cross_Platform   Instruments         3.0                7           8
9   Cross_Platform   KOJAK               0.0                8           8

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章