Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tqdm with map for Dataframes

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)
like image 779
Minions Avatar asked Sep 03 '18 15:09

Minions


People also ask

Does tqdm work with multiprocessing?

Using queues, tqdm-multiprocess supports multiple worker processes, each with multiple tqdm progress bars, displaying them cleanly through the main process.

Does tqdm work with while loops?

tqdm does not require any dependencies and works across multiple python environments. Integrating tqdm can be done effortlessly in loops, on iterable, with Pandas or even with machine learning libraries— just wrap any iterable with tqdm(iterable) , and you're done!


1 Answers

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

like image 179
Teoretic Avatar answered Sep 27 '22 17:09

Teoretic