AnalystPath

Pivot Store Sales by Quarter

PandasEasyJunior level~10 min

Problem

You are given a DataFrame `storesales` loaded from `storesales.csv` with columns `store_id`, `sales`, and `quarter`, where `quarter` is one of `Q1`, `Q2`, `Q3`, `Q4`. Each row is one store's sales in one quarter, and `(store_id, quarter)` is unique.

Reshape the data so there is exactly one row per store and a separate sales column for each of the four quarters. The output columns must be exactly `store_id`, `Q1_sales`, `Q2_sales`, `Q3_sales`, `Q4_sales`, in that order. If a store has no row for a quarter, that quarter's value must be null/NA.

Input data

Example rows — the live problem includes the full dataset.

storesales
store_idsalesquarter
1100Q1
1200Q2
250Q1

Expected output

Your answer should return 2 rows with the columns store_id, Q1_sales, Q2_sales, Q3_sales, Q4_sales.

Starter code (Pandas (Python))

import pandas as pd

def pivot_quarters(storesales) -> pd.DataFrame:
    # Your code here
    return storesales

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions