简体   繁体   中英

Conditional statement to derive value for new variable

I have a data frame that looks like this

在此处输入图像描述

# 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

在此处输入图像描述

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

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM