Machine learning

[데이터 전처리]. tqdm pandas , apply & map progress_bar 생성

Acdong 2021. 4. 15. 10:21
728x90

tqdm 은 for 문을 처리할때 

진행률을 표시해주는 파이썬 라이브러리이다.

 

하지만 pandas 에서 열전체의 함수를 매기는 map 이나 데이터프레임 전체에 함수를 매기는 apply 의 경우

많은 데이터의 실행을 했을 때 진행률을 알 수 없고

 

주피터에서 * 표시만 계속 쳐다볼뿐이다.

 

그래서 pandas 의 map 함수와 apply 함수의 진행률을 볼 수 있는 방법을 공유한다.

 

apply

import pandas as pd
import numpy as np
from tqdm import tqdm
# from tqdm.auto import tqdm  # for notebooks

df = pd.DataFrame(np.random.randint(0, int(1e8), (10000, 1000)))

# Create new `pandas` methods which use `tqdm` progress
# (can use tqdm_gui, optional kwargs, etc.)
tqdm.pandas()

# Now you can use `progress_apply` instead of `apply`
df.groupby(0).progress_apply(lambda x: x**2)

 

MAP

def sum_two(x):
	return x + 2
df['colName'] = df['colName'].progress_map(sum_two)

 

tqdm 4.8 버전 이하는 

from tqdm import tqdm, tqdm_pandas
tqdm_pandas(tqdm())

 

참고 : stackoverflow.com/questions/18603270/progress-indicator-during-pandas-operations

반응형