Skip to content Skip to sidebar Skip to footer

Create A Unique Index From Column Of Data

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 by i

Solution 1:

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

Post a Comment for "Create A Unique Index From Column Of Data"