Skip to main content
Back to problems
#1777
Easy Database

Products price for each store

Database
82.1% acceptance
Mar 31, 2026
142
13

No description available.

Solution

Pandas
Time O(1)
Space O(1)
LeetCode
solution.pandas
# Table: Products
# 
# +-------------+---------+
# | Column Name | Type    |
# +-------------+---------+
# | product_id  | int     |
# | store       | enum    |
# | price       | int     |
# +-------------+---------+
# In SQL, (product_id, store) is the primary key for this table.
# store is a category of type ('store1', 'store2', 'store3') where each represents the store this product is available at.
# price is the price of the product at this store.
# 
#  
# 
# Find the price of each product in each store.
# 
# Return the result table in any order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Products table:
# +-------------+--------+-------+
# | product_id  | store  | price |
# +-------------+--------+-------+
# | 0           | store1 | 95    |
# | 0           | store3 | 105   |
# | 0           | store2 | 100   |
# | 1           | store1 | 70    |
# | 1           | store3 | 80    |
# +-------------+--------+-------+
# Output:
# +-------------+--------+--------+--------+
# | product_id  | store1 | store2 | store3 |
# +-------------+--------+--------+--------+
# | 0           | 95     | 100    | 105    |
# | 1           | 70     | null   | 80     |
# +-------------+--------+--------+--------+
# Explanation:
# Product 0 price's are 95 for store1, 100 for store2 and, 105 for store3.
# Product 1 price's are 70 for store1, 80 for store3 and, it's not sold in store2.

import pandas as pd

def products_price(products: pd.DataFrame) -> pd.DataFrame:
  return products.pivot(index='product_id', columns='store', values='price').reset_index().rename_axis(None, axis=1)