Spaces:
Sleeping
Sleeping
Download env/tasks/task_medium.py from gauravnetes/datacleaning-env: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/spaces/gauravnetes/datacleaning-env/resolve/main/env/tasks/task_medium.py
- Command line
-
hf download hf://spaces/gauravnetes/datacleaning-env/env/tasks/task_medium.py
-
curl -L -o task_medium.py https://huggingface.co/spaces/gauravnetes/datacleaning-env/resolve/main/env/tasks/task_medium.py
2.59 kB
| import pandas as pd | |
| import numpy as np | |
| import os | |
| TASK_NAME = "normalize_and_deduplicate" | |
| DIFFICULTY = "medium" | |
| DESCRIPTION = "Standardize date formats, normalize phone numbers, and remove duplicate records." | |
| DIRTY_PATH = os.path.join(os.path.dirname(__file__), "../datasets/medium_dirty.csv") | |
| CLEAN_PATH = os.path.join(os.path.dirname(__file__), "../datasets/medium_clean.csv") | |
| def generate_datasets(): | |
| np.random.seed(7) | |
| n = 120 | |
| base_dates = pd.date_range("2020-01-01", periods=n, freq="3D") | |
| base_phones = [f"9{np.random.randint(100000000, 999999999)}" for _ in range(n)] | |
| clean = pd.DataFrame({ | |
| "customer_id": range(1, n + 1), | |
| "signup_date": base_dates.strftime("%Y-%m-%d"), | |
| "phone": base_phones, | |
| "plan": np.random.choice(["basic", "pro", "enterprise"], n), | |
| "amount": np.round(np.random.uniform(100, 5000, n), 2), | |
| }) | |
| dirty = clean.copy() | |
| # Mess up dates — mixed formats | |
| fmt_choices = ["%d/%m/%Y", "%m-%d-%Y", "%B %d, %Y"] | |
| for i in np.random.choice(n, 40, replace=False): | |
| fmt = np.random.choice(fmt_choices) | |
| dirty.loc[i, "signup_date"] = pd.to_datetime(clean.loc[i, "signup_date"]).strftime(fmt) | |
| # Mess up phones — add dashes, spaces, country code | |
| for i in np.random.choice(n, 40, replace=False): | |
| p = clean.loc[i, "phone"] | |
| style = np.random.choice(["dash", "space", "country"]) | |
| if style == "dash": | |
| dirty.loc[i, "phone"] = f"{p[:5]}-{p[5:]}" | |
| elif style == "space": | |
| dirty.loc[i, "phone"] = f"{p[:4]} {p[4:]}" | |
| else: | |
| dirty.loc[i, "phone"] = f"+91-{p}" | |
| # Inject duplicates (20 duplicate rows appended) | |
| dup_idx = np.random.choice(n, 20, replace=False) | |
| duplicates = clean.iloc[dup_idx].copy() | |
| dirty = pd.concat([dirty, duplicates], ignore_index=True) | |
| os.makedirs(os.path.dirname(DIRTY_PATH), exist_ok=True) | |
| dirty.to_csv(DIRTY_PATH, index=False) | |
| clean.to_csv(CLEAN_PATH, index=False) | |
| print(f"[datasets] medium_dirty.csv and medium_clean.csv generated.") | |
| def get_task(): | |
| if not os.path.exists(DIRTY_PATH): | |
| generate_datasets() | |
| return { | |
| "name": TASK_NAME, | |
| "difficulty": DIFFICULTY, | |
| "description": DESCRIPTION, | |
| "dirty_path": DIRTY_PATH, | |
| "clean_path": CLEAN_PATH, | |
| "allowed_operations": [ | |
| "normalize_dates", | |
| "normalize_phones", | |
| "drop_duplicates", | |
| "submit" | |
| ], | |
| "max_steps": 10, | |
| } | |
| if __name__ == "__main__": | |
| generate_datasets() |