8

Can I use tqdm progress bar with map function to loop through dataframe/series rows?

specifically, for the following case:

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)
user_007
  • 3,639
  • 2
  • 33
  • 64

1 Answers1

19

Due to the integration of tqdm with pandas you can use progress_map function instead of map function.

Note: for this to work you should add tqdm.pandas() line to your code.

So try this:

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

Here is the documentation reference:

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

Teoretic
  • 1,851
  • 14
  • 26