Skip to main content
Back to problems
#1543
Easy Database

Fix product name format

Database
58.8% acceptance
Mar 31, 2026
93
45

No description available.

Solution

Pandas
Time O(1)
Space O(1)
LeetCode
solution.pandas
# Table: Sales
# 
# +--------------+---------+
# | Column Name  | Type    |
# +--------------+---------+
# | sale_id      | int     |
# | product_name | varchar |
# | sale_date    | date    |
# +--------------+---------+
# sale_id is the column with unique values for this table.
# Each row of this table contains the product name and the date it was sold.
# 
#  
# 
# Since table Sales was filled manually in the year 2000, product_name may contain leading and/or trailing white spaces, also they are case-insensitive.
# 
# Write a solution to report
# 
# product_name in lowercase without leading or trailing white spaces.
# 
# sale_date in the format ('YYYY-MM').
# 
# total the number of times the product was sold in this month.
# 
# Return the result table ordered by product_name in ascending order. In case of a tie, order it by sale_date in ascending order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Sales table:
# +---------+--------------+------------+
# | sale_id | product_name | sale_date  |
# +---------+--------------+------------+
# | 1       | LCPHONE      | 2000-01-16 |
# | 2       | LCPhone      | 2000-01-17 |
# | 3       | LcPhOnE      | 2000-02-18 |
# | 4       | LCKeyCHAiN   | 2000-02-19 |
# | 5       | LCKeyChain   | 2000-02-28 |
# | 6       | Matryoshka   | 2000-03-31 |
# +---------+--------------+------------+
# Output:
# +--------------+-----------+-------+
# | product_name | sale_date | total |
# +--------------+-----------+-------+
# | lckeychain   | 2000-02   | 2     |
# | lcphone      | 2000-01   | 2     |
# | lcphone      | 2000-02   | 1     |
# | matryoshka   | 2000-03   | 1     |
# +--------------+-----------+-------+
# Explanation:
# In January, 2 LcPhones were sold. Please note that the product names are not case sensitive and may contain spaces.
# In February, 2 LCKeychains and 1 LCPhone were sold.
# In March, one matryoshka was sold.

import pandas as pd

def fix_name_format(sales: pd.DataFrame) -> pd.DataFrame:
  sales['product_name'] = sales['product_name'].str.strip().str.lower()
  sales['sale_date'] = pd.to_datetime(sales['sale_date']).dt.to_period('M').astype(str)
  result = sales.groupby(['product_name', 'sale_date']).size().reset_index(name='total')
  result = result.sort_values(['product_name', 'sale_date'])
  return result