社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在Python中加快过滤过程?

RGS • 4 年前 • 1771 次点击  

我编写了下面的代码,通过比较Python中的输入产品描述来过滤数据框中可用的匹配产品描述。这个过滤过程需要很多时间才能完成。你能帮我把上面的过滤逻辑加快一点吗?

df1["PRODSPLIT"] = df1["PRODUCTDESC"].str.split()
df1["INTERSECT"] = df1["PRODSPLIT"].apply(lambda x:list(set(x).intersection("input product description".split())))
df1["PRODSPLITLEN"] = df1["INTERSECT"].str.len()
df1 = df1[df1["PRODSPLITLEN"] > 0]

编辑:

def intersects_inp_prod_or_desc(self, product_desc, input_product_desc):    
product_desc_lst = product_desc.split()    
input_product_desc_lst = input_product_desc.split()     
return any([prod in input_product_desc_lst for prod in product_desc_lst])
    
df["INTERSECT"] = df["PRODUCTDESC"].apply(lambda x: self.__intersect_product_desc(x, query[0]))
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131149
文章 [ 1 ]  |  最新文章 4 年前
ewz93
Reply   •   1 楼
ewz93    4 年前

检查是否总是只检查一行中是否有单词 df1["PRODUCTDESC"] 是 input , product 或 discription ? 在这种情况下,您可以使用:

def intersects_inp_prod_or_desc(string_):
    return any([spl in ["input", "product", "description"] for spl in string_.split()])

df1["INTERSECT"] = df1["PRODUCTDESC"].apply(intersects_inp_prod_or_desc)
df1 = df1[df1["INTERSECT"]]

它会创建一个布尔列,如果为True,则包含True,如果为Not,则包含False,然后使用该列过滤数据帧。

编辑:关于运行时

我们可以做一些基本的计时,比如:

def run(rows):
    start = time.time()
    productdesc = ["something with input", "something with product", 
                   "something with description", "something else"]
    productdesc *= rows//len(productdesc)
    df1 = pandas.DataFrame(productdesc, columns=["PRODUCTDESC"])
    df1["INTERSECT"] = df1["PRODUCTDESC"].apply(intersects_inp_prod_or_desc)
    df1 = df1[df1["INTERSECT"]]
    finish = time.time()
    print(f"{rows:,} rows: {round(finish - start, 5)} seconds")


run(10)
run(100)
run(1000)
run(10000)
run(100000)
run(1000000)
run(10000000)

首先,采用不变的方法:

def intersects_inp_prod_or_desc(string_):
    return any([spl in ["input", "product", "description"] for spl in string_.split()])

# 10 rows: 0.0 seconds
# 100 rows: 0.0 seconds
# 1,000 rows: 0.01012 seconds
# 10,000 rows: 0.01013 seconds
# 100,000 rows: 0.12163 seconds
# 1,000,000 rows: 1.21986 seconds
# 10,000,000 rows: 12.12009 seconds

我们可以手动提前返回,以节省一些时间:

def intersects_inp_prod_or_desc(string_):
    split_string = string_.split()
    for split_ in split_string:
        if split_ == "input":
            return True
        if split_ == "product":
            return True
        if split_ == "description":
            return True
    return False

# 10 rows: 0.0 seconds
# 100 rows: 0.00812 seconds
# 1,000 rows: 0.00215 seconds
# 10,000 rows: 0.0102 seconds
# 100,000 rows: 0.09158 seconds
# 1,000,000 rows: 0.94362 seconds
# 10,000,000 rows: 9.40814 seconds

然而,一旦我们在数据帧中有了一些非常长的描述,比如这样(250字),它就会开始变得慢得多:

productdesc = ["something with input", "something with product", 
               "something with description", "something else", 
               "something long without key words "*50]

# 10 rows: 0.0 seconds
# 100 rows: 0.00813 seconds
# 1,000 rows: 0.01014 seconds
# 10,000 rows: 0.09424 seconds
# 100,000 rows: 0.93761 seconds
# 1,000,000 rows: 9.35188 seconds
# 10,000,000 rows: 73.0588 seconds

但我们可以通过首先检查任何关键字是否确实是描述的子字符串来修改该方法,并且只有当它是时,我们才对拆分的字符串进行拆分和迭代:

def intersects_inp_prod_or_desc(string_):
    if "input" in string_ or "product" in string_ or "description" in string_:
        split_string = string_.split()
        for split_ in split_string:
            if split_ == "input":
                return True
            if split_ == "product":
                return True
            if split_ == "description":
                return True
    return False

# 10 rows: 0.00814 seconds
# 100 rows: 0.00209 seconds
# 1,000 rows: 0.0 seconds
# 10,000 rows: 0.0102 seconds
# 100,000 rows: 0.12225 seconds
# 1,000,000 rows: 1.15786 seconds
# 10,000,000 rows: 11.52258 seconds

但请注意,在任何情况下,此额外检查都会花费我们一点时间,因此,如果没有长描述,则不会节省时间,因为预检查的额外时间可能会比跳过拆分字符串的好处更长,例如,如果您在列表中调用此修改版本,而没有再次调用长描述:

productdesc = ["something with input", "something with product",
               "something with description", "something else"]

# 10 rows: 0.0 seconds
# 100 rows: 0.0 seconds
# 1,000 rows: 0.0 seconds
# 10,000 rows: 0.01017 seconds
# 100,000 rows: 0.10158 seconds
# 1,000,000 rows: 0.95558 seconds
# 10,000,000 rows: 9.60593 seconds

一般来说,我不知道您的数据帧有多大,因为在我看来,即使是未更改的版本也非常快。它之所以慢,可能是因为可能有很多行,而且很多行都很长。在这种情况下,使用修改后的方法,该方法会预先检查任何关键字是否是字符串的子字符串,如果不是,则立即返回False。