I want to assign a sequence number to each group of my samples. An example dataset:
product | sales
-----------------
100 | 213
100 | 4343
100 | 234
203 | 231
203 | 654
304 | 6765
404 | 534
My expected output:
product | sales | sequenced number
-------------------------------------------
100 | 213 | 1
100 | 4343 | 2
100 | 234 | 3
203 | 231 | 1
203 | 654 | 2
304 | 6765 | 1
404 | 534 | 1
I tried to use this code:
df.groupby('product',as_index=False).apply(lambda x: x.loc[:,1]=(range(len(x))))
But got the error:
lambda cannot contain assignment
你想要累积计数 :
df['seq_no'] = df.groupby('product').cumcount() + 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.