Home > Software engineering >  I have this Dataframe, Using Transform Function how can I assign them index values as new row?
I have this Dataframe, Using Transform Function how can I assign them index values as new row?

Time:01-08

This is the Dataset, I want to add a column named Index which will start number for every person from 1

This provides count, but I want to provide Index values for eg: 1,2,3 for one person, and new person comes again 1,2,3,4 etc.

new_df['Index']=new_df.groupby('Name Of Reporting Person')['Name Of Reporting Person'].transform('count')

CodePudding user response:

It seems like groupby().cumcount() is what you need:

new_df['Index']=new_df.groupby('Name Of Reporting Person')['Name Of Reporting Person'].cumcount()

CodePudding user response:

use ngroup

new_df['Index'] = new_df.groupby('Name Of Reporting Person').ngroup() 1
  • Related