首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在`str.format()`中使用多个if条件

在`str.format()`中使用多个if条件
EN

Stack Overflow用户
提问于 2018-12-17 02:52:59
回答 1查看 114关注 0票数 3

我有一个类似下面的数据框架。

我检查了得分1、2、3列,并打印了各自的主题。我可以比较两列并打印相应的文本。

如何包含不同的列?

代码语言:javascript
复制
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)

我看起来像下面的类型比较,但这给出了错误。基本上,我希望如果两列满足我想要打印的两个主题的条件(scores>=1)。如果3列满足条件(scores>=1),并且我想打印文本中的3个主题,如下所示,2个条件。有没有其他方法可以比较3列并打印文本。

代码语言:javascript
复制
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

想要的输出:

EN

回答 1

Stack Overflow用户

发布于 2018-12-17 07:52:22

您的df与预期结果不匹配,它应该是这样的,以匹配您的输出:

代码语言:javascript
复制
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'])

我添加了一个循环来处理不同的标签。这个时间与输出相匹配。

代码语言:javascript
复制
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

我相信也有更简单的方法可以做到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53805431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档