pandas 行动中的进度指示器 [英] Progress indicator during pandas operations

查看:51
本文介绍了 pandas 行动中的进度指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定期对超过1500万行的数据帧执行熊猫操作,我很乐意能够访问特定操作的进度指示器.

I regularly perform pandas operations on data frames in excess of 15 million or so rows and I'd love to have access to a progress indicator for particular operations.

是否存在基于文本的熊猫拆分应用合并操作进度指示器?

Does a text based progress indicator for pandas split-apply-combine operations exist?

例如,类似:

df_users.groupby(['userID', 'requestDate']).apply(feature_rollup)

其中,feature_rollup是一个有点复杂的函数,它占用许多DF列并通过各种方法创建新的用户列.对于大型数据帧,这些操作可能需要一段时间,因此我想知道是否有可能在iPython笔记本中提供基于文本的输出,从而使我对进度进行更新.

where feature_rollup is a somewhat involved function that take many DF columns and creates new user columns through various methods. These operations can take a while for large data frames so I'd like to know if it is possible to have text based output in an iPython notebook that updates me on the progress.

到目前为止,我已经尝试了Python的规范循环进度指示器,但是它们并没有以任何有意义的方式与熊猫互动.

So far, I've tried canonical loop progress indicators for Python but they don't interact with pandas in any meaningful way.

我希望pandas库/文档中有一些被我忽略的东西,它使人们知道了split-apply-combine的进度.一种简单的实现方式可能是查看apply函数所依据的数据帧子集的总数,并将进度报告为这些子集的完整部分.

I'm hoping there's something I've overlooked in the pandas library/documentation that allows one to know the progress of a split-apply-combine. A simple implementation would maybe look at the total number of data frame subsets upon which the apply function is working and report progress as the completed fraction of those subsets.

这也许是需要添加到库中的东西吗?

Is this perhaps something that needs to be added to the library?

推荐答案

由于流行的需求,tqdm添加了对pandas的支持.与其他答案不同,此不会显着降低熊猫的速度-以下是DataFrameGroupBy.progress_apply的示例:

Due to popular demand, tqdm has added support for pandas. Unlike the other answers, this will not noticeably slow pandas down -- here's an example for DataFrameGroupBy.progress_apply:

import pandas as pd
import numpy as np
from tqdm import tqdm
# from tqdm.auto import tqdm  # for notebooks

df = pd.DataFrame(np.random.randint(0, int(1e8), (10000, 1000)))

# Create and register a new `tqdm` instance with `pandas`
# (can use tqdm_gui, optional kwargs, etc.)
tqdm.pandas()

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

如果您对它的工作方式(以及如何针对自己的回调进行修改)感兴趣,请参见 github上的示例有关pypi的完整文档,或导入模块并运行help(tqdm).

In case you're interested in how this works (and how to modify it for your own callbacks), see the examples on github, the full documentation on pypi, or import the module and run help(tqdm).

编辑

要直接回答原始问题,请替换:

To directly answer the original question, replace:

df_users.groupby(['userID', 'requestDate']).apply(feature_rollup)

具有:

from tqdm import tqdm
tqdm.pandas()
df_users.groupby(['userID', 'requestDate']).progress_apply(feature_rollup)

注意:tqdm< = v4.8 : 对于低于4.8的tqdm版本,您必须执行以下操作:

Note: tqdm <= v4.8: For versions of tqdm below 4.8, instead of tqdm.pandas() you had to do:

from tqdm import tqdm, tqdm_pandas
tqdm_pandas(tqdm())

这篇关于 pandas 行动中的进度指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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