pandas 熊猫数据框列表理解中的 If ElseIf Else 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48527420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:06:41  来源:igfitidea点击:

If ElseIf Else condition in pandas dataframe list comprehension

pythonpandas

提问by DarknessFalls

I have a dataframe with 11 columns: Status1-Status5, Time1-Time5 & Time_Min

我有一个包含 11 列的数据框:Status1-Status5、Time1-Time5 和 Time_Min

df = pd.DataFrame([[100,200,150,400,500,'a','b','a','c','a',100], [300,400,200,500,250,'b','b','c','c','c',200]], columns=['TIME_1', 'TIME_2', 'TIME_3', 'TIME_4', 'TIME_5','STATUS_1','STATUS_2','STATUS_3','STATUS_4','STATUS_5','TIME_MIN'])

I would like to reproduce a code I have in SAS currently which does the following

我想重现我目前在 SAS 中的代码,它执行以下操作

IF TIME_1 = TIME_MIN THEN STATUS = STATUS_1;
ELSE IF TIME_2 = TIME_MIN THEN STATUS = STATUS_2;
ELSE IF TIME_3 = TIME_MIN THEN STATUS = STATUS_3;
ELSE IF TIME_4 = TIME_MIN THEN STATUS = STATUS_4;
ELSE STATUS = STATUS_5;

Expected output for column STATUS would be

列 STATUS 的预期输出为

['a','c']

I tried building something along these lines (which would need to be extended with else ifs)

我尝试沿着这些路线构建一些东西(需要用 else ifs 扩展)

df['STATUS'] = [a if x == y else b for x,y,a,b in df[['TIME_MIN','TIME_1','STATUS_1','STATUS_2']]]

But this just gives an error. I'm sure it's a simple fix, but I can't quite figure it out.

但这只是给出了一个错误。我确定这是一个简单的修复,但我无法弄清楚。

回答by Vaishali

You can write a function

你可以写一个函数

def get_status(df):
    if df['TIME_1'] == df['TIME_MIN']:
        return df['STATUS_1']
    elif df['TIME_2'] == df['TIME_MIN']:
        return df['STATUS_2']
    elif df['TIME_3'] == df['TIME_MIN']:
        return df['STATUS_3']
    elif df['TIME_4'] == df['TIME_MIN']:
        return df['STATUS_4']
    else:
        return df['STATUS_5']

df['STATUS'] = df.apply(get_status, axis = 1)

Or use a very-nested np.where,

或者使用一个非常嵌套的 np.where,

df['STATUS'] = np.where(df['TIME_1'] == df['TIME_MIN'], df['STATUS_1'],\ 
        np.where(df['TIME_2'] == df['TIME_MIN'], df['STATUS_2'],\
        np.where(df['TIME_3'] == df['TIME_MIN'], df['STATUS_3'],\
        np.where(df['TIME_4'] == df['TIME_MIN'], df['STATUS_4'], df['STATUS_5']))))

回答by Alex

Not very pretty but you can use equality broadcasting with the .eqmethod.

不是很漂亮,但您可以使用.eq方法进行平等广播。

m = df.iloc[:, :5].eq(df['TIME_MIN'], axis=0)
m.columns = 'STATUS_' + m.columns.str.extract('TIME_(.*)')
df['STATUS'] = df[m].bfill(axis=1).iloc[:, 0]