2015-11-30 23:25:05 +00:00
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
2020-01-24 22:28:48 +00:00
|
|
|
from tqdm.auto import tqdm
|
2015-11-30 23:25:05 +00:00
|
|
|
|
|
|
|
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
|
|
|
|
|
2016-07-27 21:51:52 +00:00
|
|
|
# Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`
|
2019-08-26 16:36:09 +00:00
|
|
|
# (can use `tqdm.gui.tqdm`, `tqdm.notebook.tqdm`, optional kwargs, etc.)
|
2016-07-27 21:51:52 +00:00
|
|
|
tqdm.pandas(desc="my bar!")
|
2015-11-30 23:25:05 +00:00
|
|
|
|
|
|
|
# Now you can use `progress_apply` instead of `apply`
|
2016-07-27 21:51:52 +00:00
|
|
|
# and `progress_map` instead of `map`
|
2016-05-21 16:56:28 +00:00
|
|
|
df.progress_apply(lambda x: x**2)
|
|
|
|
# can also groupby:
|
|
|
|
# df.groupby(0).progress_apply(lambda x: x**2)
|
2015-11-30 23:25:05 +00:00
|
|
|
|
2016-10-31 02:19:29 +00:00
|
|
|
# -- Source code for `tqdm_pandas` (really simple!)
|
2015-11-30 23:25:05 +00:00
|
|
|
# def tqdm_pandas(t):
|
2016-05-21 16:56:28 +00:00
|
|
|
# from pandas.core.frame import DataFrame
|
|
|
|
# def inner(df, func, *args, **kwargs):
|
|
|
|
# t.total = groups.size // len(groups)
|
2015-11-30 23:25:05 +00:00
|
|
|
# def wrapper(*args, **kwargs):
|
|
|
|
# t.update(1)
|
|
|
|
# return func(*args, **kwargs)
|
2016-05-21 16:56:28 +00:00
|
|
|
# result = df.apply(wrapper, *args, **kwargs)
|
2015-11-30 23:25:05 +00:00
|
|
|
# t.close()
|
|
|
|
# return result
|
2016-05-21 16:56:28 +00:00
|
|
|
# DataFrame.progress_apply = inner
|