library(tidyverse)
library(readxl)
path = "300-399/324/CH-324 Text Cleaning.xlsx"
input = read_excel(path, range = "B2:B9")
test = read_excel(path, range = "D2:D9")
input$Level = str_remove(str_replace_all(input$Level, "(Under Ground|Upper Ground|Ground)", "\\1,"), ",$")
all.equal(input$Level, test$Level)Omid - Challenge 324
data-challenges
advanced-exercises
🔰 The Level column in the table is derived by grouping rows with the same ID.

Challenge Description
🔰 The Level column in the table is derived by grouping rows with the same ID.
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Parses the text patterns directly instead of relying on manual cleanup
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
import re
path = "300-399/324/CH-324 Text Cleaning.xlsx"
input_df = pd.read_excel(path, usecols="B", skiprows=1, nrows=7)
test_df = pd.read_excel(path, usecols="D", skiprows=1, nrows=7).rename(columns=lambda col: col.replace('.1', ''))
input_df["Level"] = input_df["Level"]\
.apply(lambda v: re.sub(r",$", "", re.sub(r"(Under Ground|Upper Ground|Ground)", r"\1,", str(v))) if pd.notna(v) else v)
print(input_df["Level"].equals(test_df["Level"])) # TrueLogic:
Reads the workbook ranges needed for the challenge
Parses the text patterns directly instead of relying on manual cleanup
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.