How to use tqdm with pandas in a jupyter notebook?

前端 未结 4 1221
走了就别回头了
走了就别回头了 2020-12-29 23:50

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 here I found t

相关标签:
4条回答
  • 2020-12-30 00:02

    I found that I had to import tqdm_notebook also. A simple example is given below that works in Jupyter notebook.

    Given you want to map a function on a variable to create a new variable in your pandas dataframe.

    # 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)
    
    0 讨论(0)
  • 2020-12-30 00:10

    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).

    In the future (>v5.1), you should be able to use a more uniform API:

    tqdm_pandas(tqdm_notebook, *args, **kwargs)
    
    0 讨论(0)
  • 2020-12-30 00:18

    If you want to use more than 1 CPU for that slow apply step, consider using swifter. As a bonus, swifter automatically enables a tqdm progress bar on the apply step. To customize the bar description, use df.swifter.progress_bar(enable=True, desc='bar description').apply(...)

    0 讨论(0)
  • 2020-12-30 00:23

    My working solution (copied form the documnetation):

    from tqdm.auto import tqdm
    tqdm.pandas()
    
    0 讨论(0)
提交回复
热议问题