Tqdm

tqdm is a Python library that allows you to add a progress bar to your loops. It’s very easy to use and can be a great help when you’re working with large datasets or when you’re running long computations.

In this notebook, I’ll show you how to use tqdm with a few examples.

Installation

You can install tqdm using pip:

pip install tqdm

Usage

To use tqdm, you just need to wrap your iterable with the tqdm function. Here’s an example:


from tqdm import tqdm

for i in tqdm(range(100)):
    # do something

This will create a progress bar that shows the progress of the loop. You can customize the progress bar by passing additional arguments to the tqdm function. For example, you can set the total number of iterations, the width of the progress bar, and the format of the progress bar.

Here’s an example that shows how to customize the progress bar:

from tqdm import tqdm

for i in tqdm(range(100), total=100, desc="Processing", ncols=100):
    # do something

This will create a progress bar with a width of 100 characters and a description that says “Processing”.

Examples

Here are a few examples that show how to use tqdm with different types of iterables:

List

from tqdm import tqdm

data = [1, 2, 3, 4, 5]

for i in tqdm(data, desc="Processing", ncols=100):
    # do something

Range


from tqdm import tqdm

for i in tqdm(range(100), desc="Processing", ncols=100):
    # do something

File

from tqdm import tqdm

with open("data.txt", "r") as f:
    for line in tqdm(f, desc="Processing", ncols=100):
        # do something

Pandas DataFrame

import pandas as pd
from tqdm import tqdm

df = pd.read_csv("data.csv")

for index, row in tqdm(df.iterrows(), total=len(df), desc="Processing", ncols=100):
    # do something