如何在 jupyter 笔记本中将 tqdm 与 Pandas 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40476680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use tqdm with pandas in a jupyter notebook?
提问by grinsbaeckchen
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 hereI found the tqdm library that provides a simple progress bar for pandas operations. There is also a Jupyter integrationthat provides a really nice progress bar where the bar itself changes over time.
我正在 jupyter notebook 中对 pandas 进行一些分析,由于我的 apply 函数需要很长时间,我希望看到一个进度条。通过这里的这篇文章,我找到了 tqdm 库,它为 Pandas操作提供了一个简单的进度条。还有一个Jupyter 集成,它提供了一个非常好的进度条,进度条本身随着时间的推移而变化。
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)
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_notebook(tqdm.pandas(desc="my bar!"))
or
或者
tqdm_notebook.pandas
but they don't work. In the definitionit 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.
应该可以工作,但是栏没有正确显示进度,并且还有额外的输出。
Any other ideas?
还有其他想法吗?
采纳答案by gaborous
You can use:
您可以使用:
tqdm_notebook().pandas(*args, **kwargs)
This is because tqdm_notebook has a delayer adapter, so it's necessary to instanciate it before accessing its methods (including class methods).
这是因为 tqdm_notebook 有一个延迟器适配器,所以在访问它的方法(包括类方法)之前需要先实例化它。
In the future (>v5.1), you should be able to use a more uniform API:
在未来(>v5.1),你应该能够使用更统一的 API:
tqdm_pandas(tqdm_notebook, *args, **kwargs)
回答by Vincenzo Lavorini
My working solution (copied form the documnetation):
我的工作解决方案(从文档中复制 ):
from tqdm.auto import tqdm
tqdm.pandas()
回答by mammykins
I found that I had to import tqdm_notebook
also. A simple example is given below that works in Jupyter notebook.
我发现我也必须导入tqdm_notebook
。下面给出了一个适用于 Jupyter Notebook 的简单示例。
Given you want to map a function on a variable to create a new variable in your pandas dataframe.
假设您想将一个函数映射到一个变量上,以在您的 Pandas 数据框中创建一个新变量。
# progress bar
from tqdm import tqdm, tqdm_notebook
# instantiate
tqdm.pandas(tqdm_notebook)
# replace map with progress_map
# where df is a pandas dataframe
df['new_variable'] = df['old_variable'].progress_map(some_function)