• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

pythonでのtqdm

date: 2022-05-17 excerpt: pythonでのtqdmの扱い方

tag: pythontqdm


pythonでのtqdmの扱い方

概要

  • プログレスバーの表示を行うツール
  • pandasとの連携もできる

インストール

$ python3 -m pip install tqdm

インターフェース

  • total
    • 全体の大きさがわからない場合に引数で値を入れるとその大きさと仮定してプログレスバーを標示する
  • desc
    • プログレスバーに標示するヒントの文字列
  • ncols
    • 幅の大きさ
  • position
    • 下から何行目に標示するか
  • disable
    • Trueを入れると表示されなくなる
    • 環境変数と紐付けて管理すると便利
      • for x in tqdm(..., disable=os.environ.get("DISABLE_TQDM", False)):

nextを利用してtqdmのカウンターを回す

  • tqdmインスタンスをiterでラップすることで、next関数を利用してカウンターを回すことができる
counter = iter(tqdm(...))
next(counter) # tqdmがインクリメントされる

具体例

  • tqdm.auto経由でimportすることでjupyterでもterminalでも表示できる
from tqdm.auto import tqdm
import itertools
import random
import time

counter = iter(tqdm(itertools.count(0), desc="sampling...", position=1, total=500, ncols=100))
for i in tqdm(range(1000), desc="working...", position=0, ncols=100):
    time.sleep(0.005)
    if random.random() <= 0.5:
        next(counter)

pandasと連携

  • tqdm.pandas()を最初に実行する必要がある

具体例

import os
from tqdm.auto import tqdm

tqdm.pandas(desc="なにかメッセージ", disable=bool(os.environ.get("DISABLE_TQDM")))
df["any"].progress_apply(func)

参考

  • github.com/tqdm/tqdm


pythontqdm Share Tweet