【问题标题】:Order the duplicates in my pandas Data frame by adding another column [duplicate]通过添加另一列来订购我的熊猫数据框中的重复项[重复]
【发布时间】:2018-06-08 03:12:21
【问题描述】:

我需要在我的数据框中使用计数器值 1、2、3 等订购重复项。输入数据框如下所示:

         Key    Amount
         xyz     1000
         xyz1    870
         xyz2    1000
         xyz3    1000
         xyz4    650

预期输出为

         Key    Amount  Duplicate_order
         xyz     1000    1
         xyz1    870     1
         xyz2    1000    2
         xyz3    1000    3
         xyz4    650     1

【问题讨论】:

    标签: python python-3.x pandas duplicates


    【解决方案1】:

    使用 cumcount

    df['duplicate_order'] = df.groupby('Amount').cumcount()+1
    
        Key  Amount  duplicate_order
    0   xyz    1000                1
    1  xyz1     870                1
    2  xyz2    1000                2
    3  xyz3    1000                3
    4  xyz4     650                1
    

    【讨论】:

      猜你喜欢
      • 2019-09-14
      • 1970-01-01
      • 2017-05-05
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      相关资源
      最近更新 更多