Pivot Weekly Vaccine Doses by Store and Vaccine Type in Pandas

Last updated: November 29, 2026

Quick Overview

Given a DataFrame of vaccine administrations (dose_id, store_id, vaccine_type, administered_at), produce a table with one row per store and week (weeks starting Sunday), one column per vaccine type with dose counts, missing combinations filled with 0, and a total column.

CVS Health
Data Manipulation (SQL/Python)
Data Scientist
CVS Health
November 29, 2026
Data Scientist
Phone Screen
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


Given a DataFrame of vaccine administrations (dose_id, store_id, vaccine_type, administered_at), produce a table with one row per store and week (weeks starting Sunday), one column per vaccine type with dose counts, missing combinations filled with 0, and a total column.

CVS pharmacies administer a large share of US flu, COVID and RSV vaccines each fall, and operations analysts build weekly store views for staffing. This is a pandas fluency check: datetime handling, pivot_table and tidy output.

What the Interviewer Expects
  • Parses timestamps and derives a Sunday-start week
  • Uses pivot_table with count and fill_value=0
  • Adds a row total without double counting
  • Returns a flat, sorted DataFrame
Key Topics to Cover
pandas pivot_table
datetime periods
reshaping
fill_value
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you include store and week combinations that had zero doses of any type?
  • How would you add week over week change per vaccine type?
  • How would you do the same in SQL?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

One row per dose. vaccine_type is a small set (flu, covid, rsv, shingles). Weeks start on Sunday, matching retail weeks. Timestamps are local store time....

Approach

Convert to datetime, map each timestamp to its week start with to_period('W-SAT') (a week ending Saturday starts Sunday), then pivot_table with aggfunc='count'. Add total by summing across the...


Submit Your Answer
Markdown supported