0

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

  • `df['m'] = df.groupby('letter').cumcount()+1` and `df['n'] = pd.Categorical(df['letter']).codes+1` will both do what you need. – Stu Sztukowski Jan 31 '23 at 14:06

0 Answers0