Home > Mobile >  Counting rows for the same date in new column
Counting rows for the same date in new column

Time:04-06

I would like to create a new column. Column should count all rows for the same date.

in:

|    date     | 
----------------
| 12.02.2000   | 
| 12.02.2000   | 
| 12.02.2000   | 
| 12.02.2000   | 
| 12.02.2000   | 
| 13.02.2000   | 
| 14.02.2000   | 
| 14.02.2000   | 

out: 
|    date     | Cumulative
---------------|-----------
| 12.02.2000   | 1
| 12.02.2000   | 2
| 12.02.2000   | 3
| 12.02.2000   | 4
| 12.02.2000   | 5
| 13.02.2000   | 1
| 14.02.2000   | 1
| 14.02.2000   | 2

CodePudding user response:

You want to use cumcount after a groupby

df.assign(Cumulative=df.groupby('date').cumcount() 1)

         date  Cumulative
0  12.02.2000           1
1  12.02.2000           2
2  12.02.2000           3
3  12.02.2000           4
4  12.02.2000           5
5  13.02.2000           1
6  14.02.2000           1
7  14.02.2000           2
  • Related