Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,71 +6,65 @@ def process_file(file):
|
|
6 |
# 1. Validate extension
|
7 |
name = file.name.lower()
|
8 |
if not name.endswith(('.xls', '.xlsx', '.xlsm')):
|
9 |
-
return "Error: Please upload .xls
|
10 |
|
11 |
-
# 2. Read without header
|
12 |
-
df = pd.read_excel(file.name, header=None)
|
13 |
-
df = df.dropna(axis=1, how="all")
|
14 |
-
# If your file has descriptive top rows, you can also do:
|
15 |
-
# df = pd.read_excel(file.name, header=None, skiprows=2) # adjust as needed :contentReference[oaicite:9]{index=9}
|
16 |
|
17 |
-
# 3.
|
18 |
-
|
19 |
"Usage", "District", "Address", "Longitude", "Latitude",
|
20 |
"Floor", "Unit", "Area", "PriceInMillion",
|
21 |
-
"
|
22 |
-
"DeliveryDate", "MemoNo."
|
23 |
]
|
24 |
-
output_df = pd.DataFrame("", index=range(len(df)), columns=
|
25 |
|
26 |
-
# 4. Map
|
27 |
-
output_df["
|
28 |
-
output_df["Floor"]
|
29 |
-
output_df["Unit"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
# 5.
|
32 |
-
output_df["
|
33 |
-
df.iloc[:, 3]
|
34 |
-
.astype(str)
|
35 |
-
.str.extract(r"(\d+\.?\d*)", expand=False) # extract numeric :contentReference[oaicite:13]{index=13}
|
36 |
-
.astype(float)
|
37 |
-
)
|
38 |
-
|
39 |
-
# 6. Map and clean PriceInMillion (remove non‐digits then convert)
|
40 |
-
output_df["PriceInMillion"] = pd.to_numeric(
|
41 |
-
df.iloc[:, 4].replace(r"[^0-9\.]", "", regex=True),
|
42 |
-
errors="coerce"
|
43 |
-
) # robust numeric conversion :contentReference[oaicite:14]{index=14}
|
44 |
-
|
45 |
-
# 7. Map PricePerSquareFeet into "District" if that’s your intended slot
|
46 |
-
output_df["District"] = df.iloc[:, 5] # Column6 → District :contentReference[oaicite:15]{index=15}
|
47 |
-
|
48 |
-
# 8. Parse InstrumentDate and derive Year & WeekNumber
|
49 |
-
output_df["InstrumentDate"] = pd.to_datetime(
|
50 |
-
df.iloc[:, 6], errors="coerce"
|
51 |
-
) # robust parsing :contentReference[oaicite:16]{index=16}
|
52 |
-
output_df["Year"] = output_df["InstrumentDate"].dt.year
|
53 |
output_df["WeekNumber"] = output_df["InstrumentDate"].dt.isocalendar().week
|
54 |
|
55 |
-
#
|
56 |
# output_df["DeliveryDate"] = ...
|
57 |
# output_df["MemoNo."] = ...
|
58 |
|
59 |
-
#
|
60 |
-
suffix
|
61 |
out_name = f"data-clean-{suffix}.xlsx"
|
62 |
-
output_df.to_excel(out_name, index=False)
|
63 |
|
|
|
|
|
64 |
return output_df, out_name
|
65 |
|
66 |
-
with gr.Blocks() as demo:
|
67 |
-
gr.Markdown("##
|
68 |
with gr.Row():
|
69 |
-
|
70 |
-
btn
|
71 |
with gr.Row():
|
72 |
-
|
73 |
-
|
74 |
-
btn.click(process_file, inputs=[
|
75 |
|
76 |
demo.launch()
|
|
|
6 |
# 1. Validate extension
|
7 |
name = file.name.lower()
|
8 |
if not name.endswith(('.xls', '.xlsx', '.xlsm')):
|
9 |
+
return "Error: Please upload a .xls, .xlsx or .xlsm file.", None
|
10 |
|
11 |
+
# 2. Read sheet 0 without header rows (so data starts at row 0)
|
12 |
+
df = pd.read_excel(file.name, header=None)
|
13 |
+
df = df.dropna(axis=1, how="all") # drop any fully empty columns
|
|
|
|
|
14 |
|
15 |
+
# 3. Prepare output schema
|
16 |
+
output_headers = [
|
17 |
"Usage", "District", "Address", "Longitude", "Latitude",
|
18 |
"Floor", "Unit", "Area", "PriceInMillion",
|
19 |
+
"PricePerSquareFeet", "InstrumentDate", "Year",
|
20 |
+
"WeekNumber", "DeliveryDate", "MemoNo."
|
21 |
]
|
22 |
+
output_df = pd.DataFrame("", index=range(len(df)), columns=output_headers)
|
23 |
|
24 |
+
# 4. Map exactly as requested:
|
25 |
+
output_df["Address"] = df.iloc[:, 0] # col 1 → Address
|
26 |
+
output_df["Floor"] = df.iloc[:, 1] # col 2 → Floor
|
27 |
+
output_df["Unit"] = df.iloc[:, 2] # col 3 → Unit
|
28 |
+
output_df["Area"] = df.iloc[:, 3] # col 4 → Area
|
29 |
+
output_df["PriceInMillion"] = pd.to_numeric(
|
30 |
+
df.iloc[:, 4]
|
31 |
+
.replace(r"[^0-9\.]", "", regex=True),
|
32 |
+
errors="coerce"
|
33 |
+
) # col 5 → PriceInMillion
|
34 |
+
output_df["PricePerSquareFeet"] = pd.to_numeric(
|
35 |
+
df.iloc[:, 5]
|
36 |
+
.replace(r"[^0-9\.]", "", regex=True),
|
37 |
+
errors="coerce"
|
38 |
+
) # col 6 → PricePerSquareFeet
|
39 |
+
output_df["InstrumentDate"] = pd.to_datetime(
|
40 |
+
df.iloc[:, 6],
|
41 |
+
errors="coerce"
|
42 |
+
) # col 7 → InstrumentDate
|
43 |
|
44 |
+
# 5. Derive Year & WeekNumber
|
45 |
+
output_df["Year"] = output_df["InstrumentDate"].dt.year
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
output_df["WeekNumber"] = output_df["InstrumentDate"].dt.isocalendar().week
|
47 |
|
48 |
+
# 6. (Leave DeliveryDate & MemoNo. blank unless you have source columns)
|
49 |
# output_df["DeliveryDate"] = ...
|
50 |
# output_df["MemoNo."] = ...
|
51 |
|
52 |
+
# 7. Generate output filename
|
53 |
+
suffix = datetime.now().strftime("%y%m%d")
|
54 |
out_name = f"data-clean-{suffix}.xlsx"
|
|
|
55 |
|
56 |
+
# 8. Save and return
|
57 |
+
output_df.to_excel(out_name, index=False)
|
58 |
return output_df, out_name
|
59 |
|
60 |
+
with gr.Blocks(title="Excel → data‑clean Mapper") as demo:
|
61 |
+
gr.Markdown("## Upload your .xls/.xlsx/.xlsm file for data‑clean mapping")
|
62 |
with gr.Row():
|
63 |
+
file_in = gr.File(label="Input File")
|
64 |
+
btn = gr.Button("Process")
|
65 |
with gr.Row():
|
66 |
+
df_out = gr.Dataframe(label="Mapped Data")
|
67 |
+
download = gr.File(label="Download Output")
|
68 |
+
btn.click(process_file, inputs=[file_in], outputs=[df_out, download])
|
69 |
|
70 |
demo.launch()
|