Select the Top-Left Block of a Pandas DataFrame with .iloc

Quick Overview

Write a pandas function that returns the first n rows and first m columns of a DataFrame by position while keeping the original labels. It tests iloc versus loc slicing, out-of-range and zero-size slices, and whether a slice shares data with the source frame.

Select the Top-Left Block of a Pandas DataFrame with .iloc

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `head_block(df, n, m)` that returns a new `pd.DataFrame` holding the first `n` rows and first `m` columns, selected purely by integer position regardless of labels. Requirements: - Use `.iloc[:n, :m]`, not label-based `.loc`. - Keep the original index labels and column names: do not reset the index or rename columns. Example: ```python df = pd.DataFrame( {'a': [1, 2, 3, 4, 5], 'b': [6, 7, 8, 9, 10], 'c': [11, 12, 13, 14, 15], 'd': [16, 17, 18, 19, 20]}, index=[10, 30, 20, 50, 40], ) head_block(df, n=2, m=3) # a b c # 10 1 6 11 # 30 2 7 12 ``` ```hint What "new" means The statement asks for a new DataFrame. Think about whether a slice of `df` owns its data or shares it with `df`, and what happens when the caller edits the result. ``` ### What a Strong Answer Covers - Selects by position with `.iloc` and keeps the original index labels and column names. - Explains which rows a label-based slice would return on an index like the example's, including how `.loc` treats the end of a slice. - Says whether the result shares memory with `df` and how to guarantee an independent frame. - Covers `n` or `m` larger than the frame, and zero or negative values. ### Follow-up Questions - What if `n` or `m` exceeds the frame's dimensions? - How would you return the bottom-right block of `n` rows and `m` columns instead? - A caller edits the returned block and sees a `SettingWithCopyWarning`. What happened, and how does your function prevent it?

Overview: Write a pandas function that returns the first n rows and first m columns of a DataFrame by position while keeping the original labels. It tests iloc versus loc slicing, out-of-range and zero-size slices, and whether a slice shares data with the source frame.

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

Implement head_block(df, n, m) that returns a new pd.DataFrame holding the first n rows and first m columns, selected purely by integer position regardless of labels.

Requirements:

  • Use .iloc[:n, :m] , not label-based .loc .
  • Keep the original index labels and column names: do not reset the index or rename columns.

Example:

df = pd.DataFrame(
    {'a': [1, 2, 3, 4, 5],
     'b': [6, 7, 8, 9, 10],
     'c': [11, 12, 13, 14, 15],
     'd': [16, 17, 18, 19, 20]},
    index=[10, 30, 20, 50, 40],
)
head_block(df, n=2, m=3)
#     a  b   c
# 10  1  6  11
# 30  2  7  12

What a Strong Answer Covers Guidance

  • Selects by position with .iloc and keeps the original index labels and column names.
  • Explains which rows a label-based slice would return on an index like the example's, including how .loc treats the end of a slice.
  • Says whether the result shares memory with df and how to guarantee an independent frame.
  • Covers n or m larger than the frame, and zero or negative values.

Follow-up Questions Guidance

  • What if n or m exceeds the frame's dimensions?
  • How would you return the bottom-right block of n rows and m columns instead?
  • A caller edits the returned block and sees a SettingWithCopyWarning . What happened, and how does your function prevent it?