Create a Unique Index from Column of Data

前端 未结 1 1724
栀梦
栀梦 2021-01-29 01:33

I have a Pandas DataFrame that has unique records, but I need to create a unique key based on one of the columns. Below is sample data and my attempt to create a second column b

1条回答
  •  时光说笑
    2021-01-29 01:48

    You can groupby on 'subid' and then call cumcount and add 1 as it starts from 0:

    In [30]:
    df['child_no'] = df.groupby('subid').cumcount()+1
    df
    Out[30]:
                    subid  child_no
    0  327598650129611740         1
    1  327598650129611740         2
    2  327559921352747760         1
    3  327676431535405027         1
    4  327676431535405027         2
    5  327676431535405027         3
    6  327662567602840733         1
    7  327778468325442201         1
    8  327777161261272775         1
    9  327777161261272775         2
    

    0 讨论(0)
提交回复
热议问题