Skip to content

2890. Reshape Data: Melt

DataFrame report

Column NameType
productobject
quarter_1int
quarter_2int
quarter_3int
quarter_4int

Instructions

  • Write a solution to reshape the data so that each row represents sales data for a product in a specific quarter.
  • The result format is in the following example.

Example

Input:

productquarter_1quarter_2quarter_3quarter_4
Umbrella417224379611
SleepingBag80093693875

Output:

productquartersales
Umbrellaquarter_1417
SleepingBagquarter_1800
Umbrellaquarter_2224
SleepingBagquarter_2936
Umbrellaquarter_3379
SleepingBagquarter_393
Umbrellaquarter_4611
SleepingBagquarter_4875

Explanation:

The DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter.

Submissions

python
import pandas as pd

def meltTable(report: pd.DataFrame) -> pd.DataFrame:
    return report.melt(id_vars='product', var_name='quarter', value_name='sales')

Explanation

Python (Pandas)
Submitted by @noeyislearning
  • import pandas as pd: This line imports the pandas library, which is a powerful tool for data manipulation and analysis in Python, and aliases it as pd.
  • def meltTable(report: pd.DataFrame) -> pd.DataFrame:
    • meltTable is the name of the function.
    • It accepts one parameter, report, which is expected to be a pandas DataFrame. The type hint pd.DataFrame indicates the expected type of the parameter and the return type of the function.
    • The function is annotated to return a DataFrame.
  • return report.melt(id_vars='product', var_name='quarter', value_name='sales'):
    • The melt method is called on the report DataFrame.
    • id_vars='product': This specifies the column(s) to use as identifier variables. In this case, the product column will remain as is, and the other columns will be "melted".
    • var_name='quarter': This sets the name of the new column that will contain the column headers from the original DataFrame, which are not specified in id_vars. Essentially, it's the name for the "variable" column in the melted DataFrame.
    • value_name='sales': This sets the name of the new column that will contain the values from the original DataFrame's columns that were melted. It's the name for the "value" column in the melted DataFrame.