【问题标题】:How can provide dynamically varying OR conditions for filtering dataframe如何为过滤数据帧提供动态变化的 OR 条件
【发布时间】:2020-09-23 08:06:57
【问题描述】:

我有一个dataframe,我使用多个条件来过滤它。代码有点像下面(recency.py)。

def recency(config):
    i = config.exclude_list

    sales = pd.read_excel(config.input_path + "klm sales UPD.xlsx")


    df_exclude = sales[(sales["Product"].str.contains(i[0])==True)
                     | (sales["Product"].str.contains(i[1])==True)                     
                     | (sales["Customer"].str.contains(i[2])==True)
                     | (sales["Customer"].str.contains(i[3])==True)
                     | (sales["Customer"].str.contains(i[4])==True)
                     | (sales["Customer"].str.contains(i[5])==True)
                     | (sales["Customer"].str.contains(i[6])==True)
                     | (sales["Remarks"].str.contains(i[7])== True)                   
                     | (sales["Net Amount"]<10)]

这是配置类(config.py):

class config:

    input_path = "/home/ram/Downloads/Data_Science/"
    exclude_list = ["SCRAP ","GFT","B-MART HOME APPLIANCES","4 ELECTRONICS & APPLIANCES","DISCOUNT CORNER","CASH CUSTOMER","CASH BILL","GIFT"]

这里是主文件(main.py):

from config import config
from Recency import recency
recency(config)

这个配置是一个python文件,我在其中定义了一个带有exclude_list的类(命名配置),这是我通过主程序发送给函数(新近度)的内容。这里的问题是我在其中包含了8个项目排除列表,这可能会在未来改变。所以我可能需要对recency function(receny.py) 进行更改,这样当创建另一个config 文件时,在排除列表中包含多于(或少于)8 个项目时,我将能够使用相同的新近功能获取 df_exclude。我可以在 python 中执行此操作有哪些可能的方法?

如果这个问题有什么不清楚的地方,请在cmets中告诉我。

【问题讨论】:

    标签: python dataframe if-statement filter conditional-statements


    【解决方案1】:

    我会按变量折叠“或”语句块:

    def recency(config):
        i = config.exclude_list
    
        sales = pd.read_excel(config.input_path + "klm sales UPD.xlsx")
    
    
        df_exclude = sales[(sales["Product"].str.contains(i[0] + '|' i[1])==True)
                         | (sales["Customer"].str.contains(i[2] + '|' + i[3] + '|' + i[4] + '|' + i[5] + '|' + i[6])==True)
                         | (sales["Remarks"].str.contains(i[7])== True)                   
                         | (sales["Net Amount"]<10)]
    
    

    【讨论】:

    • 不客气。我将为您要对其应用排除项的每个变量(exclude_product_list、exclude_customer_list 等)创建一个字符串列表,然后我将使用模式“字符串”+“|”折叠每个列表+ 'string' 并在相应的 .contains() 方法中使用结果字符串。如果您理解我的意思,请告诉我,并请为我的答案投票,以防其他用户解决此问题
    • 另一个,很好,请把上面的一个作为替代答案。 :)
    • 当然,我刚刚添加了这个替代解决方案。如果您发现它们有用,请对每个答案进行投票。谢谢你的内存。
    【解决方案2】:

    作为替代答案,我将为每个变量创建一个列表以应用过滤器:

    class config:
    
        input_path = "/home/ram/Downloads/Data_Science/"
        exclude_product_list = ["SCRAP ","GFT"]
        exclude_customer_list = ["B-MART HOME APPLIANCES","4 ELECTRONICS & APPLIANCES","DISCOUNT CORNER","CASH CUSTOMER","CASH BILL"]
        exclude_remarks_list = ["GIFT"]
    

    然后,在新近度函数中,我会将每个列表折叠成一个字符串进行过滤:

    def recency(config):
    
        # collapse values
        product_excludes = '|'.join(config.exclude_product_list)
        customer_excludes = '|'.join(config.exclude_customer_list)
        remarks_excludes = '|'.join(config.exclude_remarks_list)
    
        sales = pd.read_excel(config.input_path + "klm sales UPD.xlsx")
    
    
        df_exclude = sales[(sales["Product"].str.contains(product_excludes)==True)
                         | (sales["Customer"].str.contains(customer_excludes)==True)
                         | (sales["Remarks"].str.contains(remarks_excludes)== True)                   
                         | (sales["Net Amount"]<10)]
    

    这样,您可以云提要并自定义每个列表以以不同方式过滤。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多