Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2026 Battingchart300
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.31.0
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Data Cleaner
|
| 3 |
+
emoji: 🧹
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
|
|
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
license: mit
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Data Cleaner
|
| 13 |
+
|
| 14 |
+
Python tool that cleans messy CSV and Excel files - removes duplicates fixes dates and empty values, in one command.
|
| 15 |
+
|
| 16 |
+
Upload a file in the demo above and download the cleaned version. No API key, everything runs inside this Space.
|
| 17 |
+
|
| 18 |
+
Same tool as the [python-data-cleaner repo on GitHub](https://github.com/Battingchart300/python-data-cleaner).
|
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Data Cleaner — Hugging Face Space demo.
|
| 2 |
+
|
| 3 |
+
Upload a messy CSV or Excel file, get a cleaned one back.
|
| 4 |
+
No API key, no account: the file is cleaned inside this Space
|
| 5 |
+
and only you download the result.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import tempfile
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import pandas as pd
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def clean_text(value):
|
| 16 |
+
if isinstance(value, str):
|
| 17 |
+
return " ".join(value.split())
|
| 18 |
+
return value
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _is_text(series: pd.Series) -> bool:
|
| 22 |
+
"""Text column? Works for both the old object dtype and pandas 3 str dtype."""
|
| 23 |
+
return series.dtype == object or pd.api.types.is_string_dtype(series)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def clean_numbers(series: pd.Series) -> pd.Series:
|
| 27 |
+
"""Turn strings like '1 200,50' into numbers, if the column looks numeric."""
|
| 28 |
+
if not _is_text(series):
|
| 29 |
+
return series
|
| 30 |
+
cleaned = (
|
| 31 |
+
series.astype(str)
|
| 32 |
+
.str.replace("\u00a0", " ", regex=False)
|
| 33 |
+
.str.replace(" ", "", regex=False)
|
| 34 |
+
.str.replace(",", ".", regex=False)
|
| 35 |
+
)
|
| 36 |
+
numeric = pd.to_numeric(cleaned, errors="coerce")
|
| 37 |
+
non_empty = series.notna() & (series.astype(str).str.strip() != "")
|
| 38 |
+
if non_empty.sum() and numeric[non_empty].notna().mean() >= 0.5:
|
| 39 |
+
return numeric
|
| 40 |
+
return series
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def clean_dates(series: pd.Series) -> pd.Series:
|
| 44 |
+
"""Parse a date-like column to YYYY-MM-DD, if most values parse."""
|
| 45 |
+
if not _is_text(series):
|
| 46 |
+
return series
|
| 47 |
+
parsed = pd.to_datetime(series, errors="coerce", dayfirst=True, format="mixed")
|
| 48 |
+
non_empty = series.notna() & (series.astype(str).str.strip() != "")
|
| 49 |
+
if non_empty.sum() and parsed[non_empty].notna().mean() >= 0.5:
|
| 50 |
+
return parsed.dt.strftime("%Y-%m-%d")
|
| 51 |
+
return series
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def clean(df: pd.DataFrame) -> tuple[pd.DataFrame, dict]:
|
| 55 |
+
report = {"rows_in": len(df)}
|
| 56 |
+
df = df.dropna(how="all")
|
| 57 |
+
report["empty_rows_removed"] = report["rows_in"] - len(df)
|
| 58 |
+
df = df.apply(lambda col: col.map(clean_text))
|
| 59 |
+
for col in df.columns:
|
| 60 |
+
name = str(col).lower()
|
| 61 |
+
if "date" in name or "datum" in name:
|
| 62 |
+
df[col] = clean_dates(df[col])
|
| 63 |
+
else:
|
| 64 |
+
df[col] = clean_numbers(df[col])
|
| 65 |
+
before = len(df)
|
| 66 |
+
df = df.drop_duplicates()
|
| 67 |
+
report["duplicates_removed"] = before - len(df)
|
| 68 |
+
report["rows_out"] = len(df)
|
| 69 |
+
report["empty_cells_left"] = int(df.isna().sum().sum())
|
| 70 |
+
return df, report
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def clean_file(upload):
|
| 74 |
+
if upload is None:
|
| 75 |
+
return None, "Upload a CSV or Excel file first."
|
| 76 |
+
path = Path(upload.name)
|
| 77 |
+
if path.suffix.lower() == ".csv":
|
| 78 |
+
df = pd.read_csv(path, dtype=str, keep_default_na=False)
|
| 79 |
+
elif path.suffix.lower() in (".xlsx", ".xls"):
|
| 80 |
+
df = pd.read_excel(path, dtype=str, keep_default_na=False)
|
| 81 |
+
else:
|
| 82 |
+
return None, "Only .csv and .xlsx files are supported."
|
| 83 |
+
df = df.replace(r"^\s*$", pd.NA, regex=True)
|
| 84 |
+
cleaned, report = clean(df)
|
| 85 |
+
out = Path(tempfile.gettempdir()) / f"{path.stem}_clean{path.suffix}"
|
| 86 |
+
if out.suffix.lower() == ".csv":
|
| 87 |
+
cleaned.to_csv(out, index=False)
|
| 88 |
+
else:
|
| 89 |
+
cleaned.to_excel(out, index=False)
|
| 90 |
+
text = (
|
| 91 |
+
f"Rows: {report['rows_in']} -> {report['rows_out']}\n"
|
| 92 |
+
f"Empty rows removed: {report['empty_rows_removed']}\n"
|
| 93 |
+
f"Duplicates removed: {report['duplicates_removed']}\n"
|
| 94 |
+
f"Empty cells left: {report['empty_cells_left']}"
|
| 95 |
+
)
|
| 96 |
+
return str(out), text
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
with gr.Blocks(title="Data Cleaner") as demo:
|
| 100 |
+
gr.Markdown(
|
| 101 |
+
"# Data Cleaner\n"
|
| 102 |
+
"Upload a messy CSV or Excel file and download the cleaned version. "
|
| 103 |
+
"Duplicates, empty rows, messy dates and numbers like `1 200,50` "
|
| 104 |
+
"get fixed in one go. No API key, everything runs here."
|
| 105 |
+
)
|
| 106 |
+
with gr.Row():
|
| 107 |
+
file_in = gr.File(label="Upload CSV or Excel", file_types=[".csv", ".xlsx"])
|
| 108 |
+
file_out = gr.File(label="Download cleaned file")
|
| 109 |
+
report = gr.Textbox(label="Report")
|
| 110 |
+
btn = gr.Button("Clean it")
|
| 111 |
+
btn.click(clean_file, inputs=file_in, outputs=[file_out, report])
|
| 112 |
+
gr.Examples(
|
| 113 |
+
examples=[["examples/messy.csv"]],
|
| 114 |
+
inputs=file_in,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
openpyxl
|