【问题标题】:Python break down a super long IF statementPython 分解超长 IF 语句
【发布时间】:2023-03-24 02:25:01
【问题描述】:

如何简化或分解 Python pandas 中的超长 IF 条件?

my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
good_dataframes = []
for df_name, df in my_dataframes.items():
    if ((df.loc[1:4, 'test'] <= 6).all() 
        and (df.loc[5:9, 'dif'] < 9).all()) or ((df.loc[1:5, 'test'] <= 6).all() 
                                                      and (df.loc[5:8, 'dif'] < 8).all()) or ((df.loc[1:8, 'test'] <= 6).all() 
                                                                                                    and (df.loc[5:8, 'dif'] < 9).all()):
        good_dataframes.append(df_name)

对我来说挑战在于正确的缩进

【问题讨论】:

  • 定义一个函数。
  • @Barmar 我不是定义函数的专家,请指教
  • @PyBoss 它是def myFunction():,然后是一个选项卡式换行符和return ...,其中 ... 是您拥有的长表达式。然后if(myFunction()) 更短

标签: python python-3.x pandas if-statement


【解决方案1】:

定义一个包含条件的函数。

def is_good_df(df):
    return (((df.loc[1:4, 'test'] <= 6).all()
             and (df.loc[5:9, 'dif'] < 9).all())
            or ((df.loc[1:5, 'test'] <= 6).all()
                and (df.loc[5:8, 'dif'] < 8).all())
            or ((df.loc[1:8, 'test'] <= 6).all()
                and (df.loc[5:8, 'dif'] < 9).all()))

一个好的 IDE 应该有助于正确缩进。

请注意,在您调用了一些 .all 之后,您也错过了 ()。

那你就可以了

my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
good_dataframes = [df_name for df, df_name in my_dataframes.items() if is_good_df(df)]

【讨论】:

    【解决方案2】:

    您可以分解为 3 个主要的检查功能:

    my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
    
    def check_1(df):
        return (df.loc[1:4, 'test'] <= 6).all() and (df.loc[5:9, 'dif'] < 9).all()
    
    def check_2(df):
        return (df.loc[1:5, 'test'] <= 6).all() and (df.loc[5:8, 'dif'] < 8).all()
    
    def check_3(df):
        return (df.loc[1:8, 'test'] <= 6).all() and (df.loc[5:8, 'dif'] < 9).all()
    
    CHECK_FUNCTIONS = (check_1, check_2, check_3)
    
    def check(df):
        return any(check_f(df) for check_f in CHECK_FUNCTIONS)
    
    good_dataframes = []
    for df_name, df in my_dataframes.items():
    
        if  check(df):
            good_dataframes.append(df_name)
    

    要获得 good_dataframes,您可以使用列表推导:

    good_dataframes = [df_name for df_name, df in my_dataframes.items() if check(df)]
    

    【讨论】:

    • 最喜欢你的回答,因为我在每个函数中都有很多 IF 语句,任何函数都能满足我的需要
    【解决方案3】:

    这会让它更容易理解吗:

    my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
    good_dataframes = []
    for df_name, df in my_dataframes.items():
        if ((df.loc[1:4, 'test'] <= 6).all and (df.loc[5:9, 'dif'] < 9).all()) or 
           ((df.loc[1:5, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 8).all()) or 
           ((df.loc[1:8, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 9).all()):
            good_dataframes.append(df_name)
    

    【讨论】:

      【解决方案4】:

      您可以将and 表达式放在一个列表中,然后检查值中的any 是否为True(与您的or 逻辑匹配):

      my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
      good_dataframes = []
      for df_name, df in my_dataframes.items():
          conds = [(df.loc[1:4, 'test'] <= 6).all and (df.loc[5:9, 'dif'] < 9).all(),
                   (df.loc[1:5, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 8).all(),
                   (df.loc[1:8, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 9).all()]
          if any(conds):
              good_dataframes.append(df_name)
      

      【讨论】:

      • 是的 @PyBoss ,这是通过 any 函数完成的。在我的回答中,这是一个链接,您可以在那里阅读更多相关信息
      【解决方案5】:

      你可以使用and、or等逻辑运算

      `my_dataframes = {'d1': d1, 'd2': d2,'d3': d3} good_dataframes = [] for df_name, df in my_dataframes.items():
          conds = [(df.loc[1:4, 'test'] <= 6).all and (df.loc[5:9, 'dif'] < 9).all(),
                   (df.loc[1:5, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 8).all(),
                   (df.loc[1:8, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 9).all()]
          if any(conds):
              good_dataframes.append(df_name)
      

      `

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 2011-07-12
        • 1970-01-01
        • 2022-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 2018-08-05
        相关资源
        最近更新 更多