Categories: Power BI / Python
Tags:

The dataset is in a wide format where sales data for different brands and months are spread across multiple columns.

import pandas as pd

# Load the dataset
file_path = "your_file.xlsx"  # Replace with your actual file path
df = pd.read_excel(file_path)

# Identify the static columns
id_vars = ["Region", "ALB Code", "Channels", "POS Name"]

# Melt the DataFrame to convert it into long format
df_melted = df.melt(id_vars=id_vars, var_name="Brand_Month", value_name="Sales")

# Save the transformed dataset
df_melted.to_excel("transformed_data.xlsx", index=False)

# Display the first few rows
print(df_melted.head())