Skip to content Skip to sidebar Skip to footer

Add A Unique Identifier In A New Column Until A Condition Met On Another Column

I have a dask dataframe with npartition=8, here is the snapshot of the data: id1 id2 Page_nbr record_type St1 Sc1 3 START Sc1 St

Solution 1:

Take the "record_type" column, compare to "START", and then compute the cumsum:

ddf['group_id'] = ddf['record_type'].eq('START').cumsum()
ddf.compute()

   id1  id2  Page_nbr record_type  group_id
0  St1  Sc1         3       START         1
1  Sc1  St1         5         ADD         1
2  Sc1  St1         9       OTHER         1
3  Sc2  St2        34       START         2
4  Sc2  St2        45    DURATION         2
5  Sc2  St2        65         END         2
6  Sc3  Sc3         4       START         3

Post a Comment for "Add A Unique Identifier In A New Column Until A Condition Met On Another Column"