【问题标题】:How to use tqdm with map for Dataframes如何将 tqdm 与数据帧的地图一起使用
【发布时间】:2019-02-08 16:54:57
【问题描述】:

我可以使用带有 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)

【问题讨论】:

    标签: python-3.x pandas tqdm


    【解决方案1】:

    由于 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

    (添加tqdm.pandas()后)...你可以使用progress_apply代替applyprogress_map 而不是map

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2017-08-27
      • 2017-03-21
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多