如何在Jupyter笔记本中对 pandas 使用tqdm? [英] How to use tqdm with pandas in a jupyter notebook?

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

问题描述

我正在使用jupyter笔记本对熊猫进行一些分析,由于我的apply函数需要很长时间,因此我希望看到一个进度条. 通过这篇文章此处,我找到了tqdm库,该库为熊猫操作. 还有一个 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笔记本中对 pandas 使用tqdm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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