갈루아의 반서재

728x90

 

Introducing tqdm

tqdm 는 즉석에서 progress bar 를 생성해주고, 함수나 반복문의 TTC (Time To Completion) 를 예측하는 파이썬 패키지를 말한다.

from tqdm import tqdm_notebook
list = []
for x in tqdm_notebook(range(10000)):
list.append(x**x)

pip 를 이용해서 다음과 같이 간단히 설치가 가능하다.

(AnnaM) founder@hilbert:~$ pip install tqdm
Collecting tqdm
Downloading https://files.pythonhosted.org/packages/e1/c1/bc1dba38b48f4ae3c4428aea669c5e27bd5a7642a74c8348451e0bd8ff86/tqdm-4.36.1-py2.py3-none-any.whl (52kB)
|████████████████████████████████| 61kB 2.2MB/s
Installing collected packages: tqdm
Successfully installed tqdm-4.36.1

Conda 를 사용중인 경우 다음과 같이 설치할 수 있다.

(AnnaM) founder@hilbert:~$ conda install -c conda-forge tqdm
Collecting package metadata: done
Solving environment: done
## Package Plan ##
environment location: /home/founder/anaconda3/envs/AnnaM
added / updated specs:
- tqdm
The following packages will be downloaded:
package | build
---------------------------|-----------------
tqdm-4.36.1 | py_0 43 KB conda-forge
------------------------------------------------------------
Total: 43 KB
The following NEW packages will be INSTALLED:
tqdm conda-forge/noarch::tqdm-4.36.1-py_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
tqdm-4.36.1 | 43 KB | ######################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

 

Using tqdm

tqdm 사용 역시 간단하다. 다음과 같이 임포트하기만 하면 된다.

from tqdm import tqdm, tqdm_notebook

코드 내의 함수나 반복문을  tdqm() 또는 tqdm_notebook() 로 감싸기만 하면 된다. 

from tqdm import tnrange, tqdm_notebook
from time import sleep
for i in tqdm_notebook(range(4), desc='1st loop'):
for j in tqdm_notebook(range(100), desc='2nd loop', leave=False):
sleep(0.01)

 

But what about .apply() functions in pandas?

tqdm 을 임포트했으면, tqdm.pandas() 을 초기화가 가능하다.

from tqdm._tqdm_notebook import tqdm_notebook
tqdm_notebook.pandas()

그리고 .apply() 함수를 .progress_apply() 로 교체하면 된다.

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,(10000, 1000)))
df.progress_apply(lambda x: x**2)

 

원문소스 https://towardsdatascience.com/progress-bars-in-python-and-pandas-f81954d33bae

 

Progress Bars in Python (and pandas!)

Time and estimate the progress of your functions in Python (and pandas!)

towardsdatascience.com

 

728x90