tqdm/examples/pandas_progress_apply.py

30 lines
925 B
Python
Raw Normal View History

2015-11-30 23:25:05 +00:00
import pandas as pd
import numpy as np
from tqdm import tqdm
2015-11-30 23:25:05 +00:00
df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
# Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`
# (can use `tqdm_gui`, `tqdm_notebook`, optional kwargs, etc.)
tqdm.pandas(desc="my bar!")
2015-11-30 23:25:05 +00:00
# Now you can use `progress_apply` instead of `apply`
# 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
""" Source code for `tqdm_pandas` (really simple!) """
# 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