ZICHAO LI - 26 days ago 4x

Python Question

Given a data frame

`df`

`a b`

2 nan

3 nan

3 nan

4 nan

4 nan

4 nan

5 nan

5 nan

5 nan

5 nan

...

A critical rule is that each number

`n`

`a`

`n-1`

`a b`

2 1

3 1

3 2

4 1

4 2

4 3

5 1

5 2

5 3

5 4

...

Thus the number

`m`

`b`

`1`

`n-1`

`df.groupby('a').apply(lambda x: np.asarray(range(x['a'].unique()[0])))`

But the result is a list in one row, which is not what I want.

Could you please tell me how to implement it? Thanks in advance!

Answer

You need `cumcount`

:

```
df['b'] = df.groupby('a').cumcount() + 1
print (df)
a b
0 2 1
1 3 1
2 3 2
3 4 1
4 4 2
5 4 3
6 5 1
7 5 2
8 5 3
9 5 4
```

Source (Stackoverflow)

Comments