0

I am trying to replicate some values in a dataset. The original data that I am running my code against for verification purposes has two categories across multiple groups, like so:

grp5
0                       3941
1                        459

grp6
0                       4120
1                        280

grp7
0                       4300
1                        100

The original code that was used to create the categories across groups was written in SAS, a straightforward if-then macro statement, see below:

%macro E
IF 4 <= n <= 5
    THEN grp5 = 1; 
ELSE IF 2 <= n <= 3
    THEN grp6 = 1;
ELSE grp7 = 1;
%mend E

With my Python code I should also get the same number of cases in each category across groups, however, there are some discrepancies between the values I am getting and I'm not sure why. Below is my Python script and the values I am getting.

# Initialize columns
df['grp5'] = 0
df['grp6'] = 0
df['grp7'] = 0

# create boolean conditions
cond5 = df['n'].between(4, 5)
cond6 = df['n'].between(2, 3)

# Apply conditions
df.loc[cond5, 'grp5'] = 1
df.loc[~cond5 & cond6, 'grp6'] = 1
df.loc[~cond5 & ~cond6, 'grp7'] = 1


grp5
0                  3878
1                   522

grp6
0                  2437         
1                  1963     

grp7
0                  2485         
1                  1915 
4
  • 1
    maybe create normal function with if/elif/else and use it with df.apply(...) Commented Mar 31 at 8:53
  • 1
    condition ~cond5 & cond6 should give the same result as using only cond6, Commented Mar 31 at 9:06
  • 1
    as for me code seems OK - so the only idea is that you work with different data. maybe test it on smaller part - so you could manually compare data and see which gives different result. Commented Mar 31 at 9:08
  • Where are the values of n coming from. There could be rounding issues. Commented Mar 31 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.