2021年1月5日星期二

pandas group by and sequence

I have a data frame with a category column.

For each class within the category column, I would like a repeating sequence from 1 to n. For example, in the below table, for each unique value in the category column (e.g. a, b, c etc.), I would like to have a repeating sequence from 1 to 3 in the corresponding sequence column

id category sequence
1 a 1
2 a 2
3 a 3
4 a 1
5 a 2
6 a 3
7 b 1
8 b 2
9 b 3
10 b 1
11 b 2
12 b 3

I would very much appreciate any suggestions

https://stackoverflow.com/questions/65588677/pandas-group-by-and-sequence January 06, 2021 at 08:48AM

没有评论:

发表评论