Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add incremental counter for repeating feature subsets in pandas

Suppose I have the following table:

id name mail date
1 Sta sta@example.com 11.11.22
2 Danny dany@example.com 11.11.22
3 Elle elle@example.com 11.11.22
4 Elle falsemail@example.com 11.11.22
5 Elle elle@example.com 12.11.22

What is the best way to create an incremental counter for repeating observations for the feature subset [name, date]?

Desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

id name mail date counter
1 Sta sta@example.com 11.11.22 1
2 Danny dany@example.com 11.11.22 1
3 Elle elle@example.com 11.11.22 1
4 Elle falsemail@example.com 11.11.22 2
5 Elle elle@example.com 12.11.22 1

Edit: The table itself is sorted correctly and the duplicates appear after each other.

>Solution :

df['counter'] = df.groupby(['name', 'date']).cumcount() + 1 
df
   id   name                   mail        date  counter
0  1    Sta         sta@example.com   11.11.22         1
1  2   Danny       dany@example.com   11.11.22         1
2  3   Elle        elle@example.com   11.11.22         1
3  4   Elle   falsemail@example.com   11.11.22         2
4  5   Elle        elle@example.com   12.11.22         1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading