Melt quarterly revenue columns into long form with pandas

Quick Overview

Reshape a wide pandas DataFrame of quarterly store revenue into long form with one row per store and quarter. It tests melt with explicit identifier and value columns, exact output column names, the default index, and the row order the reshape produces.

Melt quarterly revenue columns into long form with pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def melt_quarters(df: pd.DataFrame) -> pd.DataFrame`. The input is wide with columns `['store', 'region', 'Q1', 'Q2', 'Q3', 'Q4']` (strings, then integer revenue). Unpivot only the quarter columns into long form, keeping `store` and `region` as identifiers. Requirements: - Use `id_vars=['store', 'region']` and `value_vars=['Q1', 'Q2', 'Q3', 'Q4']`. - Return columns exactly `['store', 'region', 'quarter', 'revenue']`. - Return a fresh default `RangeIndex`. Example input (wide): ```text store region Q1 Q2 Q3 Q4 0 A East 10 20 30 40 ``` Expected output (long): ```text store region quarter revenue 0 A East Q1 10 1 A East Q2 20 2 A East Q3 30 3 A East Q4 40 ``` ```hint Naming the new columns `melt` creates two new columns, one holding the old column names and one holding the values. Check which parameters control their names. ``` ### Constraints & Assumptions - `store` and `region` are strings; `Q1` to `Q4` hold integer revenue. - Each input row becomes four output rows, one per quarter. ### What a Strong Answer Covers - Unpivoting only the four quarter columns while keeping `store` and `region` on every row. - Output columns named and ordered exactly `['store', 'region', 'quarter', 'revenue']`. - A default `RangeIndex` on the result. - The row order `melt` produces when there is more than one store, and whether it matters downstream. ### Follow-up Questions - How would you sort the result by `store` then `quarter`? - How would you convert `quarter` values like `Q1` into integers 1 to 4? - How would you turn the long frame back into the wide layout?

Overview: Reshape a wide pandas DataFrame of quarterly store revenue into long form with one row per store and quarter. It tests melt with explicit identifier and value columns, exact output column names, the default index, and the row order the reshape produces.

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

Implement def melt_quarters(df: pd.DataFrame) -> pd.DataFrame. The input is wide with columns ['store', 'region', 'Q1', 'Q2', 'Q3', 'Q4'] (strings, then integer revenue). Unpivot only the quarter columns into long form, keeping store and region as identifiers.

Requirements:

  • Use id_vars=['store', 'region'] and value_vars=['Q1', 'Q2', 'Q3', 'Q4'] .
  • Return columns exactly ['store', 'region', 'quarter', 'revenue'] .
  • Return a fresh default RangeIndex .

Example input (wide):

  store region  Q1  Q2  Q3  Q4
0     A   East  10  20  30  40

Expected output (long):

  store region quarter  revenue
0     A   East      Q1       10
1     A   East      Q2       20
2     A   East      Q3       30
3     A   East      Q4       40

Constraints & Assumptions

  • store and region are strings; Q1 to Q4 hold integer revenue.
  • Each input row becomes four output rows, one per quarter.

What a Strong Answer Covers Guidance

  • Unpivoting only the four quarter columns while keeping store and region on every row.
  • Output columns named and ordered exactly ['store', 'region', 'quarter', 'revenue'] .
  • A default RangeIndex on the result.
  • The row order melt produces when there is more than one store, and whether it matters downstream.

Follow-up Questions Guidance

  • How would you sort the result by store then quarter ?
  • How would you convert quarter values like Q1 into integers 1 to 4?
  • How would you turn the long frame back into the wide layout?