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

Column with # of times index is repeated

I have a pandas DataFrame in which some rows are repeated hence they have the same index

Example:

        A
0.      34
1.      12 
1.      12 
2.      21
2.      21
2.      21

How can I create a column "B" which contains how many times that index was repeated?

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

Desired Output:

        A.    B.
0.      34.   1
1.      12    1
1.      12    2
2.      21.   1
2.      21.   2
2.      21.   3

>Solution :

You can create a dummy column of 1s and groupby the index and use cumsum on the dummy column:

df['B'] = df.assign(one=1).groupby(level=0)['one'].cumsum()

Another option is to use groupby the index and use cumcount (and add 1) to get the running count:

df['B'] = df.groupby(level=0).cumcount()+1

Output:

      A  B
0.0  34  1
1.0  12  1
1.0  12  2
2.0  21  1
2.0  21  2
2.0  21  3
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