【问题标题】:Nested if formula in pythonpython中的嵌套if公式
【发布时间】:2020-08-28 09:44:09
【问题描述】:

我正在尝试在 pandas 中执行嵌套 if(连同 AND & OR 函数),我有以下两个数据框

dF1
TR_ID  C_ID  Code  Check1 Check2
1      101   P1     N       Y
2      102   P2     Y       Y
3      103   P3     N       Y
4      104   P4     Y       N
5      105   P5     N       N
6      106   P6     Y       Y
7      107   P7     N       N
8      108   P8     N       N
9      109   P9     Y       Y
10     110   P10    Y       N

dF2
C_ID  CC
101   A1
102   A2
103   A3
104   A4
105   A5
106   A6
107   A7
108   A8
109   A9
110   A10

我正在尝试使用下面的 excel 公式在 Df1 中创建一个新列“结果”,我对 Pandas Python 编码相当陌生,

Excel 公式 =

IF(AND(OR($D2="P2",$D2="P4",$D2="P6",$D2="P9"),$E2="Y",$F2="Y"),"A11",VLOOKUP($C2,$J$2:$K$11,2,0))'

生成的数据框应如下所示

TR_ID  C_ID  Code  Check1 Check2  RESULT
1      101   P1     N       Y        A1
2      102   P2     Y       Y        A11
3      103   P3     N       Y        A3
4      104   P4     Y       N        A4
5      105   P5     N       N        A5
6      106   P6     Y       Y        A11
7      107   P7     N       N        A7
8      108   P8     N       N        A8
9      109   P9     Y       Y        A11
10     110   P10    Y       N        A10

我正在 python 中尝试此代码df1['CC'] = df1['Code'].apply(lambda x: 'A11' if x in ('P2','P4','P6','P9') else 'N')

但我无法合并 check1 和 Check2 标准,否则 vlookup 不起作用。

非常感谢任何建议

【问题讨论】:

  • 请解释一下检查 1 和 2 的作用。让条件更清楚一点,并假设受访者在excel方面是菜鸟

标签: python excel pandas numpy if-statement


【解决方案1】:

试试这个:

# This is the first part of your IF statement
cond = (
    df1['Code'].isin(['P2', 'P4', 'P6', 'P9'])
        & df1['Check1'].eq('Y')
        & df1['Check2'].eq('Y')
)

# And the VLOOKUP
# (but don't name your dataframe `vlookup` in production code please
vlookup = df1[['C_ID']].merge(df2, on='C_ID')

# Combining the two
df1['RESULT'] = np.where(cond, 'All', vlookup['CC'])

【讨论】:

  • 谢谢,太好了
【解决方案2】:

与不将工作表或单元格区域视为数据集对象的 Excel 不同,Pandas 允许您与具有命名列和属性的数据进行交互。

因此,考虑使用DataFrame.merge 后跟一个条件逻辑,例如Series.where 计算类似于IF 公式。此外,~ 运算符否定逻辑条件。

p_list = ['P2', 'P4', 'P6', 'P9']

final_df = dF1.merge(dF2, on = "C_ID")

final_df['Result'] = final_df['CC'].where(~((final_df['Code'].isin(p_list))
                                             & (final_df['Check1'] == 'Y')
                                             & (final_df['Check2'] == 'Y')
                                             ), 'A11')

print(final_df)
#    TR_ID  C_ID Code Check1 Check2   CC Result
# 0      1   101   P1      N      Y   A1     A1
# 1      2   102   P2      Y      Y   A2    A11
# 2      3   103   P3      N      Y   A3     A3
# 3      4   104   P4      Y      N   A4     A4
# 4      5   105   P5      N      N   A5     A5
# 5      6   106   P6      Y      Y   A6    A11
# 6      7   107   P7      N      N   A7     A7
# 7      8   108   P8      N      N   A8     A8
# 8      9   109   P9      Y      Y   A9    A11
# 9     10   110  P10      Y      N  A10    A10

Online Demo (点击顶部的运行)

【讨论】:

  • 非常感谢,这更有意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多