How would you iterate over rows in a DataFrame, and what's the difference between iterrows() and itertuples()?

Quick Overview

Asks how to loop over the rows of a pandas DataFrame and how iterrows() and itertuples() differ. It tests what each method returns per row, how that choice affects speed and column dtypes, and when a vectorized operation should replace the loop entirely.

How would you iterate over rows in a DataFrame, and what's the difference between iterrows() and itertuples()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How would you iterate over rows in a pandas DataFrame, and what's the difference between `iterrows()` and `itertuples()`? ```hint Look at the row object Think about what kind of object each method builds for every row. A row can mix columns of different types, so ask what each container does with that. ``` ### What a Strong Answer Covers - What each method yields per row and how you read a column value from it - Why one method is slower and how building a row object can change dtypes - What happens to column names that are not valid Python identifiers - When to skip row iteration entirely and use vectorized operations ### Follow-up Questions - Why doesn't assigning to `row["col"]` inside an `iterrows()` loop change the DataFrame, and how would you update values correctly? - Is `df.apply(func, axis=1)` faster than an explicit loop? When would you still use it? - How would you compute a per-row value that depends on a condition across two columns without looping?

Overview: Asks how to loop over the rows of a pandas DataFrame and how iterrows() and itertuples() differ. It tests what each method returns per row, how that choice affects speed and column dtypes, and when a vectorized operation should replace the loop entirely.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How would you iterate over rows in a pandas DataFrame, and what's the difference between iterrows() and itertuples()?

What a Strong Answer Covers Guidance

  • What each method yields per row and how you read a column value from it
  • Why one method is slower and how building a row object can change dtypes
  • What happens to column names that are not valid Python identifiers
  • When to skip row iteration entirely and use vectorized operations

Follow-up Questions Guidance

  • Why doesn't assigning to row["col"] inside an iterrows() loop change the DataFrame, and how would you update values correctly?
  • Is df.apply(func, axis=1) faster than an explicit loop? When would you still use it?
  • How would you compute a per-row value that depends on a condition across two columns without looping?