【问题标题】:Using multiple if conditions with `str.format()`在 `str.format()` 中使用多个 if 条件
【发布时间】:2019-05-17 05:48:26
【问题描述】:

我有一个如下所示的数据框。

我检查 score1,2,3 列并打印相应的主题。我可以比较 2 列并打印相应的文本。

如何包含不同的列?

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)

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

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

想要的输出:

【问题讨论】:

  • 为什么要标记 python2 和 3?您是否同时使用?您的问题是否与处理一个问题有关,而不是另一个问题,您需要同时处理这两个问题吗?
  • 任何版本都适合我。这就是我标记两个版本的原因。
  • 我有未标记的 2.7 版本。谢谢
  • 这里的逻辑不清楚,你能解释一下吗?
  • 预期输出是什么?

标签: python python-3.x pandas


【解决方案1】:

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

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'])

我添加了一个循环来处理不同的标签。这次是匹配输出。

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

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

【讨论】:

  • 这在其他数据帧中不起作用。如果 df_[col][row] >= 1: 我在这一行得到 Keyerror:0
  • 这仅适用于您提供的数据框。它基本上是使用分数列创建的 de 中的列号并将其应用于原始数据框,其中前三列是您想要从中获取信息的列。您需要将其调整为我提供的代码。此外,将输入和输出数据框添加到问题中。我假设您正在研究提供的那个。
  • @KumarAK 预期输出与 S_scores 的数据框中的数据不匹配。检查我对答案的修改。
猜你喜欢
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 2013-06-17
  • 1970-01-01
相关资源
最近更新 更多