0

I have the following dataframe:

ID    Name    Study   City   Grade
1     Vir     BE      Ahm     1
2     Vish    ME      Rjk     2
3     Keval   B.sc    Beng    4
4     Vir     M.sc    Ahm     1
5     Priya   B.com   Una     2
6     Vish    ME      Rjk     2

I want to Increment number of Group_ID column based on Name, City and Grade columns. What is the best way to get the result of the dataframe below?

ID    Name    Study   City   Grade   Group_ID
1     Vir     BE      Ahm     1        501
2     Vish    ME      Rjk     2        502
3     Keval   B.sc    Beng    4        503
4     Vir     M.sc    Ahm     1        501
5     Priya   B.com   Una     2        504
6     Vish    ME      Rjk     2        502

  • 2
    `df['Group_ID'] = df.groupby(['Name', 'City', 'Grade'], sort=False).ngroup() + 501` as seen [here](https://stackoverflow.com/a/67873779/15497888). Just add 501 to move the base group number from 0 (the default) to 501 to match your output. – Henry Ecker Feb 12 '22 at 03:18
  • @HenryEcker Please check my question regarding this if you can clear my doubts... https://stackoverflow.com/questions/71866414/pandas-groupby-by-three-columns-with-cumsum-or-cumcount-or-nggroup – daily update Apr 14 '22 at 04:16

0 Answers0