Home > Mobile >  Column with # of times index is repeated
Column with # of times index is repeated

Time:02-18

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?

Desired Output:

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

CodePudding user response:

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
  • Related