在pandas数据框中有多个if else条件,并派生多个列 [英] multiple if else conditions in pandas dataframe and derive multiple columns

查看:598
本文介绍了在pandas数据框中有多个if else条件,并派生多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下数据框.

import pandas as pd
import numpy as np
raw_data = {'student':['A','B','C','D','E'],
        'score': [100, 96, 80, 105,156], 
    'height': [7, 4,9,5,3],
    'trigger1' : [84,95,15,78,16],
    'trigger2' : [99,110,30,93,31],
    'trigger3' : [114,125,45,108,46]}

df2 = pd.DataFrame(raw_data, columns = ['student','score', 'height','trigger1','trigger2','trigger3'])

print(df2)

我需要基于多个条件派生Flag列.

I need to derive Flag column based on multiple conditions.

我需要将得分和身高列与触发器1 -3列进行比较.

i need to compare score and height columns with trigger 1 -3 columns.

标志列:

  1. 如果得分大于等于触发器1并且高度小于8,则为红色-

  1. if Score greater than equal trigger 1 and height less than 8 then Red --

如果得分大于等于触发器2并且高度小于8,则为黄色-

if Score greater than equal trigger 2 and height less than 8 then Yellow --

如果得分大于等于触发器3并且高度小于8,则表示橙色-

if Score greater than equal trigger 3 and height less than 8 then Orange --

如果高度大于8,则将其保留为空白

if height greater than 8 then leave it as blank

如果在pandas数据框中有其他条件并导出列,该如何写?

How to write if else conditions in pandas dataframe and derive columns?

预期产量

  student  score  height  trigger1  trigger2  trigger3    Flag
0       A    100       7        84        99       114  Yellow
1       B     96       4        95       110       125     Red
2       C     80       9        15        30        45     NaN
3       D    105       5        78        93       108  Yellow
4       E    156       3        16        31        46  Orange

对于我最初的问题中的其他列Text1,我已经厌倦了这个问题,但是在使用astype(str)进行连接时,整数列未转换字符串吗?

For other column Text1 in my original question i have tired this one but the interger columns not converting the string when concatenation using astype(str) any other approach?

def text_df(df):

    if (df['trigger1'] <= df['score'] < df['trigger2']) and (df['height'] < 8):
        return df['student'] + " score " + df['score'].astype(str) + " greater than " + df['trigger1'].astype(str) + " and less than height 5"
    elif (df['trigger2'] <= df['score'] < df['trigger3']) and (df['height'] < 8):
        return df['student'] + " score " + df['score'].astype(str) + " greater than " + df['trigger2'].astype(str) + " and less than height 5"
    elif (df['trigger3'] <= df['score']) and (df['height'] < 8):
        return df['student'] + " score " + df['score'].astype(str) + " greater than " + df['trigger3'].astype(str) + " and less than height 5"
    elif (df['height'] > 8):
        return np.nan

推荐答案

您需要使用上下限进行链式比较

You need chained comparison using upper and lower bound

def flag_df(df):

    if (df['trigger1'] <= df['score'] < df['trigger2']) and (df['height'] < 8):
        return 'Red'
    elif (df['trigger2'] <= df['score'] < df['trigger3']) and (df['height'] < 8):
        return 'Yellow'
    elif (df['trigger3'] <= df['score']) and (df['height'] < 8):
        return 'Orange'
    elif (df['height'] > 8):
        return np.nan

df2['Flag'] = df2.apply(flag_df, axis = 1)

    student score   height  trigger1    trigger2    trigger3    Flag
0   A       100     7       84          99          114         Yellow
1   B       96      4       95          110         125         Red
2   C       80      9       15          30          45          NaN
3   D       105     5       78          93          108         Yellow
4   E       156     3       16          31          46          Orange

注意:您可以使用非常嵌套的np.where来执行此操作,但是我更喜欢将函数应用于多个if-else

Note: You can do this with a very nested np.where but I prefer to apply a function for multiple if-else

这篇关于在pandas数据框中有多个if else条件,并派生多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆