【问题标题】:Return value based on condition from multiple columns根据多列条件返回值
【发布时间】:2020-10-23 08:45:26
【问题描述】:

我有一个包含 4 列的数据框,基本上我正在尝试使用 if 语句创建另一列并返回满足条件的值

If NR/HL1 is not equal to 0, then outputColumn(NR/HL) = NR/HL1

if NR/HL1 is equals to 0, then outputColumn(NR/HL) = NR/HL2

if NR/HL1 is equals to 0 and NR/HL2 is equal to 0, then outputColumn(NR/HL) = NR/HL3
SKU NR/HL1 NR/HL2 NR/HL3  OutputColumn(NR/HL)
123  10     20     0         10
456  0      30     20        30
567  0      0      40        40
890  10     20     50        10

我使用了下面的代码,它工作正常,但不是 100% 准确。总是错过一个或另一个条件。如果您检查输出条件 3 的图像是否满足但它返回默认值。 NR/HL3 !=0, But still NR/HL ==0

def f(AC_off_trade):
    if AC_off_trade['NR/HL1'] != 0:
        return AC_off_trade['NR/HL1']
    if AC_off_trade['NR/HL1'] == 0:
        val = AC_off_trade['NR/HL2']
    if AC_off_trade['NR/HL1'] == 0 and AC_off_trade['NR/HL2'] == 0:
        return AC_off_trade['NR/HL3']
    else:
        return 0
AC_off_trade['NR/HL'] = AC_off_trade.apply(f,axis=1)

更新代码

#defining condition
hl1_equal_0_condition = AC_off_trade["NR/HL1"]==0.0    
hl2_equal_0_contition = AC_off_trade["NR/HL2"]==0.0
#default value
AC_off_trade.loc[:,"NR/HL"]=0
#setting values depending on condition
AC_off_trade.loc[~hl1_equal_0_condition, "NR/HL"] = AC_off_trade["NR/HL1"]
AC_off_trade.loc[hl1_equal_0_condition, "NR/HL"] = AC_off_trade["NR/HL2"]
AC_off_trade.loc[hl1_equal_0_condition & hl2_equal_0_contition, "NR/HL"] = AC_off_trade["NR/HL3"]

【问题讨论】:

    标签: python excel pandas numpy


    【解决方案1】:

    首先,您应该尽量避免使用apply 来支持矢量化,因为性能要好得多。然后你可以使用条件来获得你想要的输出:

    df = pd.DataFrame({"SKU": [123, 456, 567, 890], "NR/HL1" : [10, 0, 0, 10], "NR/HL2": [20, 30, 0, 20],"NR/HL3": [0, 20, 40, 50]})
    
    # Defining conditions 
    hl1_equal_0_condition = df["NR/HL1"]==0    
    hl2_equal_0_contition = df["NR/HL2"]==0
    
    # Setting the default value
    df.loc[:,"NR/HL"] = 0
    
    # Setting the values deppeding on tje conditions
    df.loc[~hl1_equal_0_condition, "NR/HL"] = df["NR/HL1"]
    df.loc[hl1_equal_0_condition, "NR/HL"] = df["NR/HL2"]
    df.loc[hl1_equal_0_condition & hl2_equal_0_contition, "NR/HL"] = df["NR/HL3"]
    

    输出:

        SKU NR/HL1  NR/HL2  NR/HL3  NR/HL
    0   123 10      20      0       10
    1   456 0       30      20      30
    2   567 0       0       40      40
    3   890 10      20      50      10
    

    【讨论】:

    • 感谢您的提示和代码,但它仍然面临同样的问题。条件 3 不起作用。 NR/HL1 and NR/HL2 = 0 but NR/HL3 != 0 but NR/HL return 0。我已将屏幕截图图像附加到问题中
    • 条件 3 对我有用,应该还有别的东西。你也可以给我看代码吗?您是否更改了条件的顺序?你应该保持原样。另一件事,您的列类型(NR/HL2NR/HL1)可能是一个字符串并且在比较时会失败?
    • 成功了,一列中有字符串。我将str 更改为```int`。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2022-01-15
    • 2016-09-30
    • 2015-06-06
    相关资源
    最近更新 更多