Using multiple if conditions with `str.format()`

Viewed 118

I have a dataframe like below.

I check score1,2,3 columns and print the respective subjects. I can compare 2 columns and print the respective text.

How do I include a different column?

import pandas as pd
import numpy as np

raw_data = {'Sub1':['A','B','C','D','E'],
            'Sub2':['F','G','H','I','J'],
            'Sub3':['K','L','M','N','O'],
    'S_score1': [1, 0, 0, 6,0], 
    'F_score1' : [0, 1,0,0,0],
    'L_score1' : [1,2,3,0,4],
    'S_score2': [0, 0, 0, 6,0], 
    'F_score2' : [0, 1,0,0,0],
    'L_score2' : [1,2,3,0,4],
    'S_score3': [0, 0, 0, 6,0], 
    'F_score3' : [0, 1,0,0,0],
    'L_score3' : [1,2,3,0,4]}

df2 = pd.DataFrame(raw_data, columns = ['Sub1','Sub2','Sub3','S_score1', 'F_score1','L_score1','S_score2', 'F_score2','L_score2','S_score3', 'F_score3','L_score3'])

def S_text(f):
    s_text = "You have scored on {}" .format(f['Sub1']) if f['S_score1'] >= 1 else "You have scored on {}" .format(f['Sub2'])
    return s_text

def F_text(f):
    f_text = "You have scored on {}" .format(f['Sub1']) if f['F_score1'] >= 1 else "You have scored on {}" .format(f['Sub2'])
    return f_text

def L_text(f):
    l_text = "You have scored on {}" .format(f['Sub1']) if f['L_score1'] >= 1 else "You have scored on {}" .format(f['Sub2'])
    return l_text

df2['s_text'] = df2.apply(S_text, axis=1)
df2['f_text'] = df2.apply(F_text, axis=1)
df2['l_text'] = df2.apply(L_text, axis=1)

and i am looking like like below type comparison but this giving error. basically i want if 2 columns satisfy the conditions(scores>=1) i wanted to print 2 respective subjects. if 3 columns satisfy the conditions(scores>=1) and i wanted to print 3 subjects in text like below for 2 conditions. is there any alternative way to compare 3 columns and print the text.

def S_text(f):
    s_text = "You have scored on {}" .format(f['Sub1']) if f['S_score1'] >= 1 
    elif  f['S_score2'] >= 1 "You have scored on {}" .format(f['Sub2']) 
    elif f['S_score3'] >=1 "You have scored on {}" .format(f['Sub3']) 
    elif f['S_score1'] >=1 and f['S_score2']>=1 "You have scored on {} {}" .format(f['Sub1'], f['Sub2'])
    elif f['S_score1'] >=1 and f['S_score3']>=1 "You have scored on {} {}" .format(f['Sub1'], f['Sub3'])
    elif f['S_score2'] >=1 and f['S_score3']>=1 "You have scored on {} {}" .format(f['Sub2'], f['Sub3'])
    elif f['S_score1'] >=1 and f['S_score2']>=1 and f['S_score3']>=1 "You have scored on {} {} {}" .format(f['Sub1'],f['Sub2'], f['Sub3'])
    return s_text

wanted output:

output

1 Answers

Your df does not match the expected results, it should be like this to match your output:

raw_data = {'Sub1':['A','B','C','D','E'],
            'Sub2':['F','G','H','I','J'],
            'Sub3':['K','L','M','N','O'],
    'S_score1': [1, 0, 0, 6,0], 
    'F_score1' : [0, 1,0,0,0],
    'L_score1' : [1,2,3,0,4],
    'S_score2': [0, 1, 0, 6,0], 
    'F_score2' : [0, 1,0,0,0],
    'L_score2' : [1,2,3,0,4],
    'S_score3': [0, 1, 0, 6,0], 
    'F_score3' : [0, 1,0,0,0],
    'L_score3' : [1,2,3,0,4]}

df2 = pd.DataFrame(raw_data, columns = ['Sub1','Sub2','Sub3','S_score1', 'F_score1','L_score1','S_score2', 'F_score2','L_score2','S_score3', 'F_score3','L_score3'])

I added a loop to deal with the different labels. This time is matching the output.

import re
letters = ['S', 'F', 'L']
for letter in letters:
    for row in range(0, len(df2)):
        df_ = df2[[letter+'_score1', letter+'_score2', letter+'_score3']].iloc[row:row+1,0:3]
        row_string = ''
        for col in df_.columns:
            if df_[col][row] >= 1:
                row_string = row_string + ', '+ str(df2.iloc[row][df_.columns.get_loc(col)])
            row_string = re.sub(r'^,', '', row_string)
        if row_string == '':
            df2.loc[row:,letter+'_text'] = 'You have not Scored any subject'
        else:
            df2.loc[row:,letter+'_text'] = 'You have scored on' + row_string
display(df2))


    Sub1    Sub2    Sub3    S_score1    F_score1    L_score1    S_score2    F_score2    L_score2    S_score3    F_score3    L_score3    S_text  F_text  L_text
0   A   F   K   1   0   1   0   0   1   0   0   1   You have scored on A    You have not Scored any subject You have scored on A, F, K
1   B   G   L   0   1   2   1   1   2   1   1   2   You have scored on G, L You have scored on B, G, L  You have scored on B, G, L
2   C   H   M   0   0   3   0   0   3   0   0   3   You have not Scored any subject You have not Scored any subject You have scored on C, H, M
3   D   I   N   6   0   0   6   0   0   6   0   0   You have scored on D, I, N  You have not Scored any subject You have not Scored any subject
4   E   J   O   0   0   4   0   0   4   0   0   4   You have not Scored any subject You have not Scored any subject You have scored on E, J, O

I am sure there are easier ways to do this too.

Related