Recently, I am converting from SAS to Python pandas. One question I have is that does pandas have a retain like function in SAS.
My SAS code is:
data df1;
retain col3 " ";
set df;
by ID ;
if first.ID then col3=col1;
else col3=col3;
the other condition i have is in SAS code is:
data df1;
retain col3;
set df;
by ID ;
if first.ID then col3=1;
else col3=col3+1;
Next SAS code which i am trying to convert is shown below:
proc sql;
create table t1 as
select
c1, c2, c3, c4, c5, flag, max(flag) as MAX_flag
from t1
group by c1, c2, c3, c5;
run;
I tried it in pandas as below but seems i am making some silly mistakes. If anyone know how to replicate above sas code block3 into pandas
t1=t1[['c1','c2','c3','c4','c5','c6']]
t1.loc[:,'Max_flag']=t1['flag'].max()
t1.groupby(['c1','c2','c3','c5'])
In my eg. col3 is B and col1 is a. The condition on which it should be done is df.groupby(['ID'],as_index=False).first()
I have 2 columns ID, A. my requirement is Input data df1:
ID A
1 a
1 b
2 c
1 p
2 q
Output dataframe should have one more column name as B. It will group by ID.first(). and copy the data of col A to col B for all the grouped ID.
Output should be df1
ID A B
1 a a
1 b a
2 c c
1 p a
2 q c
**My key requirement is to convert above SAS code to Pnadas**