Search code examples
pythonpandastqdm

How to add a description to tqdm pandas?


I would like to put a small description in front of (or after, does not really matter) the tqdm pandas progress bars, somthing like below:

import numpy as np
import pandas as pd
from tqdm.auto import tqdm; tqdm.pandas()

a = pd.Series(np.arange(100))

squares = a.progress_map(lambda x: x**2) #this one works
cubes = a.progress_map(lambda x: x**3) #this one works

squares = a.progress_map(lambda x: x**2, desc = 'Computing squares...') #this one doesn't work
cubes = a.progress_map(lambda x: x**3, desc = 'Computing cubes...') #this one doesn't work

So, how can I add a description to progress bars?


Solution

  • Maybe this:

    import numpy as np
    import pandas as pd
    from tqdm.auto import tqdm
    
    a = pd.Series(np.arange(100))
    
    tqdm.pandas(desc='Computing squares')
    squares = a.progress_map(lambda x: x**2)
    
    tqdm.pandas(desc='Computing cubes')
    cubes = a.progress_map(lambda x: x**3)
    

    Output:

    Computing squares: 100%|██████████| 100/100 [00:00<00:00, 37766.11it/s]
    Computing cubes: 100%|██████████| 100/100 [00:00<00:00, 30211.80it/s]