library(tidyverse)
library(readxl)
path = "files/200-299/232/CH-232 Table Transformation.xlsx"
input = read_excel(path, range = "B2:B18")
test = read_excel(path, range = "D2:F6") %>%
arrange(Price)
result = input %>%
mutate(col = row_number() %% 2, row = (row_number() + 1) %/% 2) %>%
pivot_wider(names_from = col, values_from = `Column 1`) %>%
separate(
`1`,
into = c("Product", "Measure"),
sep = "\\s",
extra = "merge"
) %>%
select(-row) %>%
pivot_wider(names_from = Measure, values_from = `0`) %>%
select(Code, Product, Price) %>%
mutate(Price = as.numeric(Price), Code = as.numeric(Code)) %>%
arrange(Price)
all.equal(result, test, check.attributes = FALSE)
#> [1] TRUEOmid - Challenge 232
data-challenges
advanced-exercises
🔰 Table Transformation!

Challenge Description
🔰 Table Transformation!
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Reshapes the data into the grain required by the task
Builds the intermediate columns that drive the final result
Strengths:
- The R solution stays close to the workbook rule and keeps the transformation compact.
Areas for Improvement:
- The code assumes the sheet structure and source ranges remain stable.
Gem:
- The strongest part of the solution is choosing the right intermediate representation before shaping the final output.
import pandas as pd
path = "200-299/232/CH-232 Table Transformation.xlsx"
input_df = pd.read_excel(path, usecols="B", skiprows=1, nrows=16)
test = pd.read_excel(path, usecols="D:F", skiprows=1, nrows=4).sort_values("Price").reset_index(drop=True)
df = input_df.copy()
df['col'] = df.index % 2
df['row'] = df.index // 2 + 1
wide = df.pivot(index='row', columns='col', values='Column 1').reset_index(drop=True)
wide.columns = wide.columns.astype(str)
wide[['Product', 'Measure']] = wide['0'].str.split(n=1, expand=True)
result = wide.drop(columns=['0']).pivot(index='Product', columns='Measure', values='1').reset_index()
result = result.rename_axis(None, axis=1)
result = result[['Code', 'Product', 'Price']]
result['Price'] = pd.to_numeric(result['Price'])
result['Code'] = pd.to_numeric(result['Code'])
result = result.sort_values('Price').reset_index(drop=True)
print(result.equals(test)) # TrueLogic:
Reads the workbook ranges needed for the challenge
Reshapes the data into the grain required by the task
Strengths:
- The Python version follows the same rule in a direct dataframe-oriented implementation.
Areas for Improvement:
- The code assumes the workbook layout remains stable, so any sheet redesign would require small adjustments.
Gem:
- The implementation stays close to the original workbook rule instead of adding unnecessary abstraction.
Difficulty Level
This task is moderate:
The core logic is clear, but the correct transformation pattern is not obvious from the raw input.
The challenge combines multiple reshaping, grouping, or parsing steps.