Select rows and columns by label in a given order with pandas .loc

Quick Overview

Write a pandas function that returns the sub-frame for a list of row labels and a list of column names, in the order requested and with the original dtypes. It tests label-based indexing with .loc, the difference from positional .iloc, missing labels, and copies versus views.

Select rows and columns by label in a given order with pandas .loc

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `select_cells(df, labels, cols)` that returns the sub-frame at row index `labels` and column names `cols`. The DataFrame has a string index (for example `'a', 'b', 'c', 'd'`). Requirements: - Use label-based `.loc`, not positional `.iloc`. - Return rows and columns in the exact order given in `labels` and `cols`. - Preserve the original column dtypes. Example: ```python df = pd.DataFrame( {"x": [1, 2, 3, 4], "y": [10, 20, 30, 40], "z": [0.1, 0.2, 0.3, 0.4]}, index=["a", "b", "c", "d"], ) select_cells(df, ["c", "a"], ["z", "x"]) # z x # c 0.3 3 # a 0.1 1 ``` ```hint Two axes, two arguments Think about what each position inside the indexer selects, and what kind of argument you pass on each axis so the result stays a DataFrame. ``` ### Constraints & Assumptions - The index holds string labels. - Every requested label and column exists, so no validation for missing labels is required. ### What a Strong Answer Covers - Selecting rows by index label and columns by name in a single indexing step - Returning rows and columns in the order given by `labels` and `cols` - Why passing lists on both axes gives a DataFrame and keeps each column's dtype - How label-based `.loc` differs from positional `.iloc` ### Follow-up Questions - What happens if a label in `labels` is missing? - Does the selection return a view or a copy? - If the index held integers, how would `df.loc[[0, 1]]` and `df.iloc[[0, 1]]` differ?

Overview: Write a pandas function that returns the sub-frame for a list of row labels and a list of column names, in the order requested and with the original dtypes. It tests label-based indexing with .loc, the difference from positional .iloc, missing labels, and copies versus views.

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

Implement select_cells(df, labels, cols) that returns the sub-frame at row index labels and column names cols. The DataFrame has a string index (for example 'a', 'b', 'c', 'd').

Requirements:

  • Use label-based .loc , not positional .iloc .
  • Return rows and columns in the exact order given in labels and cols .
  • Preserve the original column dtypes.

Example:

df = pd.DataFrame(
    {"x": [1, 2, 3, 4], "y": [10, 20, 30, 40], "z": [0.1, 0.2, 0.3, 0.4]},
    index=["a", "b", "c", "d"],
)
select_cells(df, ["c", "a"], ["z", "x"])
#      z  x
# c  0.3  3
# a  0.1  1

Constraints & Assumptions

  • The index holds string labels.
  • Every requested label and column exists, so no validation for missing labels is required.

What a Strong Answer Covers Guidance

  • Selecting rows by index label and columns by name in a single indexing step
  • Returning rows and columns in the order given by labels and cols
  • Why passing lists on both axes gives a DataFrame and keeps each column's dtype
  • How label-based .loc differs from positional .iloc

Follow-up Questions Guidance

  • What happens if a label in labels is missing?
  • Does the selection return a view or a copy?
  • If the index held integers, how would df.loc[[0, 1]] and df.iloc[[0, 1]] differ?