如何在 jupyter 笔记本中将 tqdm 与 Pandas 一起使用? [英] How to use tqdm with pandas in a jupyter notebook?

查看:45
本文介绍了如何在 jupyter 笔记本中将 tqdm 与 Pandas 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 jupyter notebook 中对 Pandas 进行一些分析,由于我的 apply 函数需要很长时间,我希望看到一个进度条.通过这篇文章这里我找到了 tqdm 库,它提供了一个简单的进度条pandas 操作.还有一个 Jupyter 集成,它提供了一个非常好的进度条,进度条本身随时间变化.

I'm doing some analysis with pandas in a jupyter notebook and since my apply function takes a long time I would like to see a progress bar. Through this post here I found the tqdm library that provides a simple progress bar for pandas operations. There is also a Jupyter integration that provides a really nice progress bar where the bar itself changes over time.

但是,我想将两者结合起来,但不太明白如何做到这一点.让我们以与文档中相同的示例

However, I would like to combine the two and don't quite get how to do that. Let's just take the same example as in the documentation

import pandas as pd
import numpy as np
from tqdm import tqdm

df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))

# Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`
# (can use `tqdm_gui`, `tqdm_notebook`, optional kwargs, etc.)
tqdm.pandas(desc="my bar!")

# Now you can use `progress_apply` instead of `apply`
# and `progress_map` instead of `map`
df.progress_apply(lambda x: x**2)
# can also groupby:
# df.groupby(0).progress_apply(lambda x: x**2)

它甚至说可以使用'tqdm_notebook'",但我没有找到方法.我尝试了一些类似的事情

It even says "can use 'tqdm_notebook' " but I don't find a way how. I've tried a few things like

tqdm_notebook(tqdm.pandas(desc="my bar!"))

tqdm_notebook.pandas

但它们不起作用.在 定义 中,它看起来像

but they don't work. In the definition it looks to me like

tqdm.pandas(tqdm_notebook(desc="my bar!"))

应该可以,但是进度条没有正确显示进度,而且还有额外的输出.

should work, but the bar doesn't properly show the progress and there is still additional output.

还有其他想法吗?

推荐答案

您可以使用:

tqdm_notebook().pandas(*args, **kwargs)

这是因为 tqdm_notebook 有一个延迟器适配器,所以在访问它的方法(包括类方法)之前需要先实例化它.

This is because tqdm_notebook has a delayer adapter, so it's necessary to instanciate it before accessing its methods (including class methods).

在未来(>v5.1)中,您应该能够使用更统一的 API:

In the future (>v5.1), you should be able to use a more uniform API:

tqdm_pandas(tqdm_notebook, *args, **kwargs)

这篇关于如何在 jupyter 笔记本中将 tqdm 与 Pandas 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆