python-3.x - 如何将tqdm与Dataframes的 map 一起使用

标签 python-3.x pandas tqdm

我可以使用带有 map 功能的tqdm进度栏来遍历数据框/系列行吗?

具体来说,对于以下情况:

def example(x):
    x = x + 2
    return x

if __name__ == '__main__':
    dframe = pd.DataFrame([{'a':1, 'b': 1}, {'a':2, 'b': 2}, {'a':3, 'b': 3}])
    dframe['b'] = dframe['b'].map(example)

最佳答案

由于tqdm与 Pandas 集成,因此可以使用progress_map函数代替map函数。

注意:为此,您应该在代码中添加tqdm.pandas()行。

,请尝试以下操作:

from tqdm import tqdm

def example(x):
    x = x + 2
    return x

tqdm.pandas()  # <- added this line

if __name__ == '__main__':
    dframe = pd.DataFrame([{'a':1, 'b': 1}, {'a':2, 'b': 2}, {'a':3, 'b': 3}])
    dframe['b'] = dframe['b'].progress_map(example)  # <- progress_map here

这是documentation reference :

(after adding tqdm.pandas()) ... you can use progress_apply instead of apply and progress_map instead of map

关于python-3.x - 如何将tqdm与Dataframes的 map 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52153037/

相关文章:

linux - 如何在(linux)ubuntu16.04上设置anaconda的代理

python - 如何使用turtle-graphics模块同步动画?

python - 比较除一列之外的所有列的值

Python:如何在嵌套循环中用下一个替换 tqdm 进度条?

python - xls 到 JSON 使用 python3 xlrd

python - Ubuntu16.04下virtualenv中psutil的安装

Python Pandas 用顶行替换标题

python - 基于pandas中的相等性计算数据的拼写长度

python tqdm 多个进度条

python - 将自定义变量/统计信息添加到 tqdm 栏