我可以使用带有 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 useprogress_apply
instead ofapply
andprogress_map
instead ofmap
关于python-3.x - 如何将tqdm与Dataframes的 map 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52153037/