tqdm

Chalsu Chalsu·2021년 12월 25일
0

Python Issue

목록 보기
2/4

tqdm

  • 반복문에서 진행상황을 모니터링하기 위해 사용하는 파이썬 라이브러리
  • print 문을 통해 진행상황을 모니터링 할 수 있지만 스크롤 압박 문제로 인해 도입
# Library import
from tqdm import tqdm

반복문 종류

  • For Loop
  • Apply Loop

For Loop

# Iterrow
for i,row in tqdm(data.iterrows(), total=data.shape[0]):
		"""
		Loop
	
		"""
  • For 문에서 tqdm 라이브러리를 활용하는 방법은 다음과 같습니다.
  • iterrow를 통해 Progress Bar를 만듭니다. (일반적으로 Pandas DataFrame에선 일반적인 For 문보단 Pandas Iterrows를 활용하는게 좋다고 합니다.)

Apply Loop

tqdm.pandas()
data[column].progress_apply(lambda x : function(x))
  • Apply Loop에서 tqdm 라이브러리를 활용하는 방법은 다음과 같습니다.
  • apply 문에 적용하기 위해 먼저 tqdm.pandas()를 먼저 호출해야 합니다.
  • apply 대신 progress_apply를 활용하면 Progress Bar를 만들 수 있습니다.
- Progress Bar를 그릴 때 한 줄에 표시되지 않고 이렇게 여러 줄에 나뉘어 출력되는 경우 - tqdm instance가 이미 실행되어 있어 생기는 문제 - tqdm instance를 종료시켜 주면 원래대로 잘 돌아간다.
while len(tqdm._instances) > 0:
    tqdm._instances.pop().close()

2 Loops Progress Bar

  • 2중 for문을 돌리게 되는 경우가 존재하는데 이 때 첫 번째 Loop와 두 번째 Loop를 동시에 보여주고 싶은 경우가 발생
  • tqdm parameter 중에 desc가 존재하는데 이는 각 Progress Bar에 description을 적을 수 있는 인자이다.
for n, q in tqdm(df.iterrows(), total = len(df), desc="First loop"):
		"""
		First Loop
		"""
		for ID in tqdm(data, desc="Second loop"):
		    """
				Second Loop
				"""
profile
https://github.com/MrLee5693

0개의 댓글