TutorialsPythonLoops — for and while

Loops — for and while

Iterate over data, automate repetitive tasks, and process multiple files with loops

Loops are how you automate repetitive tasks in Python. A data analyst uses loops to process multiple Excel files, iterate over rows for complex logic, or build summary tables. However, in Pandas, you should use vectorised operations instead of loops wherever possible — they are 100x faster. This tutorial teaches both: when to use loops, and when to let Pandas handle iteration internally.

Examples

for loops — the most common pattern
# Iterate over a list
regions = ["Delhi", "Noida", "Gurgaon"]
for region in regions:
    print(f"Processing {region}")

# Iterate with index
for i, region in enumerate(regions):
    print(f"{i+1}. {region}")
# 1. Delhi
# 2. Noida
# 3. Gurgaon

# range() — loop a fixed number of times
for i in range(5):       # 0, 1, 2, 3, 4
    print(i)

for i in range(1, 6):    # 1, 2, 3, 4, 5
    print(i)

# Loop over dictionary
sales = {"Delhi": 450000, "Noida": 320000}
for city, amount in sales.items():
    print(f"{city}: ₹{amount:,}")

# Loop over multiple lists simultaneously
months = ["Jan", "Feb", "Mar"]
totals = [410000, 385000, 520000]
for month, total in zip(months, totals):
    print(f"{month}: ₹{total:,}")
💡 enumerate() gives you both the index and the value — cleaner than tracking a counter manually.
Practical: process multiple Excel files
import os
import pandas as pd

# Load and combine all CSV files in a folder
folder = "monthly_reports/"
all_dfs = []

for filename in os.listdir(folder):
    if filename.endswith(".csv"):
        path = os.path.join(folder, filename)
        df = pd.read_csv(path)
        df["source_file"] = filename   # track which file
        all_dfs.append(df)

combined = pd.concat(all_dfs, ignore_index=True)
print(f"Loaded {len(all_dfs)} files, {len(combined)} total rows")

# NOTE: For operating on DataFrame columns, avoid loops:
# SLOW:
for i, row in df.iterrows():
    df.at[i, "Tax"] = row["Amount"] * 0.18

# FAST (vectorised — 100x faster):
df["Tax"] = df["Amount"] * 0.18

Key Points

  • for loops iterate over any iterable: list, dict, range, DataFrame columns
  • enumerate() gives index + value. zip() pairs two lists together
  • Never use iterrows() on DataFrames for calculations — use vectorised operations
  • Use loops for: file I/O, building lists, complex multi-step row logic
  • List comprehensions replace simple for loops with one line

Practice Question

You need to add a "Tax" column to a 100,000-row DataFrame where Tax = Amount × 0.18. Which is the correct approach?

Related Topics

Functions in PythonWrite reusable functions to avoid repeating logic across your analysis scriptsFile Handling in PythonRead and write text files, CSV files, and automate file operations for data pipelinesExcel Automation with PythonAutomate Excel report generation, formatting, and multi-sheet workbooks with openpyxl