在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列。

我需要比较触发器1 -3列的得分和身高列。

标志栏:

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

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

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

  4. 如果高度大于8,则留空

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

预期产量

  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)进行串联时,整数列未转换字符串吗?

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

回答:

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

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

以上是 在pandas数据帧中存在多个if else条件,并派生多个列 的全部内容, 来源链接: utcz.com/qa/418339.html

回到顶部