Home > Blockchain >  Conditional statement to derive value for new variable
Conditional statement to derive value for new variable

Time:09-24

I have a data frame that looks like this

enter image description here

# sample data
df = pd.DataFrame({'id':[1,2,3,4,5],
                   'cfs':['REFER', 'DECLINE','PASS','REFER','REFER'],
                   'ofs':['DECLINE','DECLINE', 'PASS','REFER','PASS'],
                   'ffs':['PASS','DECLINE','PASS','REFER','PASS']})

I am trying to create conditional statements that will create this output

enter image description here

This is my work in progress and needless to say there have been many other attempts.

# If else condition approach
if all df['cfs'] == 'DECLINE' | df['ofs'] == 'DECLINE' | df['ffs'] == 'DECLINE':
       df['md'] =='DECLINE'
else:
    if df['cfs'] == 'PASS' & df['ofs'] == 'PASS' & df['ffs'] == 'PASS':
           df['md'] =='PASS'
else:
    if any df['cfs'] == 'REFER'
        if df['ofs'] == 'REFER'
            if df['ffs'] == 'REFER':
                df['md'] =='REFER'
                
else: df['md'] =='REFER'

This solution results in a syntaxerror: invalid syntax

There are 4 conditions to account for across 3 variables:

  1. If cfs, ofs or ffs = decline, md = decline
  2. If cfs, ofs and ffs = pass, md = pass
  3. If cfs, ofs and ffs = refer, md = refer
  4. If cfs, ofs and ffs have combinations of pass and refer, md = refer

CodePudding user response:

You wrote

    if any df['cfs'] == 'REFER'

which is missing some ( ) parentheses and a : colon:

    if any(df['cfs'] == 'REFER'):

You might find it instructive to play around with those functions in the REPL:

$ python
Python 3.10.6 | packaged by conda-forge ...
>>>
>>> any([1, 1, 0])
True
>>> 
>>> all([1, 1, 0])
False

CodePudding user response:

Posting solution in case it might help others, most restrictive as last set of logic

df.loc[(df['cfs'] == 'REFER') | (df['ofs'] == 'REFER') | (df['ffs'] == 'REFER'), 'md'] = 'REFER'
df.loc[(df['cfs'] == 'PASS') & (df['ofs'] == 'PASS') & (df['ffs'] == 'PASS'), 'md'] = 'PASS'
df.loc[(df['cfs'] == 'DECLINE') | (df['ofs'] == 'DECLINE') | (df['ffs'] == 'DECLINE'), 'md'] = 'DECLINE'
  • Related