0

Im trying to convert some old SAS Code to python. I have a conditional statement embbeded in my data step for SAS and want to do a similar thing but in python. SAS Code Below (variable names and values are generic names)

DATA DF1;
SET DF;
IF (A = '1' AND B ^= '2') OR (A='3' AND C ^= '4');
IF D ('5','6','7') AND E >= 8 THEN DELETE;
run;

Python Code as far as I got.

indexNames= DF[((DF['A'] !='1')
                           & (DF['B']== '2'))
                         | ((DF['A'] !='3')
                           & (DF['C']== '4'))].index
DF.drop(indexNames , inplace =True)

indexNames=DF[(DF['F'] == '1')
            | (DF['F'] == '2')
           |(DF['F] =='3')
           & (DF['G'] >= 4)].index

DF.drop(indexNames , inplace = True)

Im not sure this is correct or optimized if anyone has suggestions. I would appreciate it!

Lee
  • 1,427
  • 9
  • 17

1 Answers1

0

DATA DF1;
SET DF;
IF (A = '1' AND B ^= '2') OR (A='3' AND C ^= '4');
IF D ('5','6','7') AND E >= 8 THEN DELETE;
run;

could be translated into:


df -> pd.DataFrame 
df = df[!((df.A == '1' & df.B == '2') | (df.A == '3' & df.C == '4')) | !(df.D.isin('5','6','7') & df.E >= 8)]


s3nh
  • 546
  • 2
  • 11