Take this df
:
df = pd.DataFrame({'client_id':[0, 0, 0, 1, 1, 1, 2, 2, 2],
'key':['0_382','0_382','0_356','1_365',float('nan'),'1_365',float('nan'),'2_284','2_405'],
'operation':['buy','sell','sell','buy','transfer','buy','fee','buy','buy']})
client_id key operation
0 0 0_382 buy
1 0 0_382 sell
2 0 0_356 sell
3 1 1_365 buy
4 1 NaN transfer
5 1 1_365 buy
6 2 NaN fee
7 2 2_284 buy
8 2 2_405 buy
I need to create a column named pos_id
that will give an incremental value (1,2,3...) for each row, for unique values of client_id
and key
, and using a conditional to skip transfer
and fee
values of operation
.
The result should be like this:
client_id key operation pos_id
0 0 0_382 buy 1
1 0 0_382 sell 1
2 0 0_356 sell 2
3 1 1_365 buy 1
4 1 NaN transfer NaN
5 1 1_365 buy 1
6 2 NaN fee NaN
7 2 2_284 buy 1
8 2 2_405 buy 2
question from:
https://stackoverflow.com/questions/65946342/how-do-i-count-inside-a-dataframe-using-unique-values-and-conditional 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…