Pivot Table Creation
Pandas data engineering interview problem. Difficulty: beginner. Pattern: Pivot. About 12 minutes. Free to practice.
Pivot long product amounts to wide columns per date. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A long table has 1 row for each date and product. A pivot table turns it into a wide table with 1 row per date and 1 column per product, which is easier to read. From the DataFrame long (with date, product and amount), create a pivot table where each row is a date, each column is a product, and each cell is the sum of amount for that date and product. Show 0 for combinations that have no sales. Reset the index so that date is a normal column, not the index. Save your final table in result.
Requirements
- Use dates as rows and products as columns.
- Each cell is the sum of amount for that date and product.
- Fill missing combinations with 0.
- Reset the index so date is a column.
Constraints
- The input is long with the columns date, product and amount.
- Use pivot_table(index='date', columns='product', values='amount', aggfunc='sum', fill_value=0).
- The product columns are A, B and C, as they appear in the data.
Examples
Input: long sales Output: date | A | B | C 2024-01-01 | 10.0 | 5.0 | 0.0 2024-01-02 | 7.0 | 0.0 | 3.0 Missing product/date pairs become 0.
Topics: lakebench, pandas, pivot_table.
More interview problems · All interview problems · Learn data engineering
Pivot Table Creation
Interview-style drill: Pivot long product amounts to wide columns per date.
Pivot `long` so dates are rows, products are columns, values are summed `amount`, missing cells 0. Reset index so `date` is a column. Assign `result`.