I would write an instruction, in Pandas, to add two variables, "m" e "n", on a dataframe, so that:
letter | m | n |
---|---|---|
a | 1 | 1 |
a | 2 | 1 |
a | 3 | 1 |
a | 4 | 1 |
b | 1 | 2 |
b | 2 | 2 |
c | 1 | 3 |
c | 2 | 3 |
c | 3 | 3 |
c | 4 | 3 |
Something like the SAS instruction:
proc sort data=base; by letter; run;
data base; set base;
if letter =lag(letter) then m+1;
else do;
m=1; n+1;
end;
run;
May you help me? Thank you Giorgio