如何让 tqdm 在 pandas 应用上工作?

发布于 2025-01-10 07:13:41 字数 389 浏览 2 评论 0原文

Tqdm 文档显示了 tqdm 使用 Progress_apply 处理 pandas apply 的示例。我从这里改编了以下代码 https://tqdm.github.io/docs/tqdm/ 通常需要几分钟才能执行的进程(func1 是正则表达式函数)。

from tqdm import tqdm
tqdm.pandas()
df.progress_apply(lambda x: func1(x.textbody), axis=1)

生成的进度条不显示任何进度。它只是从循环开始时的 0 跳到循环结束时的 100。我当前正在运行 tqdm 版本 4.61.2

Tqdm documentation shows an example of tqdm working on pandas apply using progress_apply. I adapted the following code from here https://tqdm.github.io/docs/tqdm/ on a process that regularly take several minutes to perform (func1 is a regex function).

from tqdm import tqdm
tqdm.pandas()
df.progress_apply(lambda x: func1(x.textbody), axis=1)

The resulting progress bar doesn't show any progress. It just jumps from 0 at the start of the loop to 100 when it is finished. I am currently running tqdm version 4.61.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2025-01-17 07:13:41

将 tqdm 与 pandas 结合使用

一般来说,人们在对列或行执行操作时倾向于使用 lambda。这可以通过多种方式来完成。

  • 请注意:如果您使用 jupyter 笔记本,则应该使用 tqdm_notebook 而不是 tqdm。
  • 另外,我不确定你的代码是什么样的,但如果你只是简单地遵循 tqdm 文档中给出的示例,并且你只执行 100 次交互,那么计算机速度很快,并且会在你的进度条有时间之前就完成它。更新。也许使用像我下面提供的更大的数据集会更有启发性。

示例 1:

from tqdm import tqdm # version 4.62.2
import pandas as pd # version 1.4.1
import numpy as np

tqdm.pandas(desc='My bar!') # lots of cool paramiters you can pass here. 
# the below line generates a very large dataset for us to work with. 
df = pd.DataFrame(np.random.randn(100000000, 4), columns=['a','b','c','d'])
# the below line will square the contents of each element in an column-wise 
# fashion 
df.progress_apply(lambda x: x**2)

输出:

输出

示例 2:

# you could apply a function within the lambda expression for more complex 
# operations. And keeping with the above example... 

tqdm.pandas(desc='My bar!') # lots of cool paramiters you can pass here. 
# the below line generates a very large dataset for us to work with. 
df = pd.DataFrame(np.random.randn(100000000, 4), columns=['a','b','c','d'])

def function(x):
    return x**2
     
df.progress_apply(lambda x: function(x))

Utilizing tqdm with pandas

Generally speaking, people tend to use lambdas when performing operations on a column or row. This can be done in a number of ways.

  • Please note: that if you are working in jupyter notebook you should use tqdm_notebook instead of tqdm.
  • Also I'm not sure what your code looks like but if you're simply following the example given in the tqdm docs, and you're only performing 100 interations, computers are fast and will blow through that before your progress bar has time to update. Perhaps it would be more instructive to use a larger dataset like I provided below.

Example 1:

from tqdm import tqdm # version 4.62.2
import pandas as pd # version 1.4.1
import numpy as np

tqdm.pandas(desc='My bar!') # lots of cool paramiters you can pass here. 
# the below line generates a very large dataset for us to work with. 
df = pd.DataFrame(np.random.randn(100000000, 4), columns=['a','b','c','d'])
# the below line will square the contents of each element in an column-wise 
# fashion 
df.progress_apply(lambda x: x**2)

Output:

Output

Example 2:

# you could apply a function within the lambda expression for more complex 
# operations. And keeping with the above example... 

tqdm.pandas(desc='My bar!') # lots of cool paramiters you can pass here. 
# the below line generates a very large dataset for us to work with. 
df = pd.DataFrame(np.random.randn(100000000, 4), columns=['a','b','c','d'])

def function(x):
    return x**2
     
df.progress_apply(lambda x: function(x))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文