-1

There is a column in my dataset that looks like that:

col1
100
100
100
101
101
102
102
103
103
103
103
104
104

I want to create a column that gives an increasing number per group. Specifically, where is 100 in the col1 there will be 01. The next 100 will have 02 and so on. When it reaches to the row that has 101 it will perform similarly:01, the next 101, 02 like it did with 100.

I tried it and I can't make it do what I am planning: I have to make a new column first

df['nc'] = df.groupby(col1)

which is wrong.

Desired output:

    col1   nc
    100    01
    100    02
    100    03
    101    01
    101    02
    102    01
    102    02
    103    01
    103    02
    103  ........ and so on
    103
    104
    104
user122244
  • 109
  • 1
  • 2
  • 10
  • Sorry, I don't understand your logic. Can you [edit](https://stackoverflow.com/posts/50834192/edit) your question with an example of your desired output? – jpp Jun 13 '18 at 09:53
  • I updated it. It's clearer. – user122244 Jun 13 '18 at 09:58
  • Possible duplicate: [Pandas: conditional rolling count](https://stackoverflow.com/questions/25119524/pandas-conditional-rolling-count) – jpp Jun 13 '18 at 10:03

1 Answers1

4

I think you're looking for this.

df['nc'] = df.groupby('col1').cumcount()+1

Which gives:

   col1  nc
0   100   1
1   100   2
2   100   3
3   101   1
4   101   2
5   102   1
6   102   2
7   103   1
8   103   2
9   103   3
10  103   4
11  104   1
12  104   2

You can format the numbers as necessary if you require leading zeros.

Ben
  • 856
  • 5
  • 9