leadingbridge commited on
Commit
dfdfa21
·
verified ·
1 Parent(s): 86bed75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -37
app.py CHANGED
@@ -6,62 +6,71 @@ 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 a .xls, .xlsx or .xlsm file.", None
10
 
11
- # 2. Read first sheet
12
- df = pd.read_excel(file.name, sheet_name=0)
 
 
 
13
 
14
- # 3. Prepare output headers
15
- output_headers = [
16
  "Usage", "District", "Address", "Longitude", "Latitude",
17
  "Floor", "Unit", "Area", "PriceInMillion",
18
  "InstrumentDate", "Year", "WeekNumber",
19
  "DeliveryDate", "MemoNo."
20
  ]
21
- output_df = pd.DataFrame("", index=range(len(df)), columns=output_headers)
22
 
23
- # 4. Column‑by‑column mapping
24
- # Column 1 → Usage
25
- output_df["Usage"] = df.iloc[:, 0]
26
- # Column 2 → Floor
27
- output_df["Floor"] = df.iloc[:, 1]
28
- # Column 3 → Unit
29
- output_df["Unit"] = df.iloc[:, 2]
30
- # Column 4 → Area
31
- output_df["Area"] = df.iloc[:, 3]
32
- # Column 5 → PriceInMillion
33
- output_df["PriceInMillion"] = df.iloc[:, 4]
34
- # Column 6 → District (mapped from PricePerSquareFeet)
35
- output_df["District"] = df.iloc[:, 5]
36
- # Column 7 → InstrumentDate
37
- output_df["InstrumentDate"] = pd.to_datetime(df.iloc[:, 6], errors="coerce")
38
 
39
- # 5. Derive Year & WeekNumber from InstrumentDate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  output_df["Year"] = output_df["InstrumentDate"].dt.year
41
  output_df["WeekNumber"] = output_df["InstrumentDate"].dt.isocalendar().week
42
 
43
- # 6. (Optional) leave DeliveryDate & MemoNo. blank
44
- # or map from other columns if available:
45
- # output_df["DeliveryDate"] = pd.to_datetime(df.get("DeliveryDate", pd.NA))
46
- # output_df["MemoNo."] = df.get("MemoNo.", "")
47
-
48
- # 7. Generate output filename: data-clean-YYMMDD.xlsx
49
- date_suffix = datetime.now().strftime("%y%m%d")
50
- out_name = f"data-clean-{date_suffix}.xlsx"
51
 
52
- # 8. Save to Excel
 
 
53
  output_df.to_excel(out_name, index=False)
54
 
55
  return output_df, out_name
56
 
57
- with gr.Blocks(title="Excel → data‑clean Mapper") as demo:
58
- gr.Markdown("## Upload your Excel file (.xls/.xlsx/.xlsm) for data‑clean mapping")
59
  with gr.Row():
60
- file_in = gr.File(label="Input File")
61
  btn = gr.Button("Process")
62
  with gr.Row():
63
- df_out = gr.Dataframe(label="Mapped Data")
64
- download = gr.File(label="Download Mapped File")
65
- btn.click(fn=process_file, inputs=[file_in], outputs=[df_out, download])
66
 
67
  demo.launch()
 
6
  # 1. Validate extension
7
  name = file.name.lower()
8
  if not name.endswith(('.xls', '.xlsx', '.xlsm')):
9
+ return "Error: Please upload .xls/.xlsx/.xlsm file.", None
10
 
11
+ # 2. Read without header, drop blank columns, and skip header rows if needed
12
+ df = pd.read_excel(file.name, header=None) # read raw data :contentReference[oaicite:7]{index=7}
13
+ df = df.dropna(axis=1, how="all") # drop fully empty cols :contentReference[oaicite:8]{index=8}
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. Define output schema
18
+ headers = [
19
  "Usage", "District", "Address", "Longitude", "Latitude",
20
  "Floor", "Unit", "Area", "PriceInMillion",
21
  "InstrumentDate", "Year", "WeekNumber",
22
  "DeliveryDate", "MemoNo."
23
  ]
24
+ output_df = pd.DataFrame("", index=range(len(df)), columns=headers)
25
 
26
+ # 4. Map positional columns via iloc
27
+ output_df["Usage"] = df.iloc[:, 0] # Column1 → Usage :contentReference[oaicite:10]{index=10}
28
+ output_df["Floor"] = df.iloc[:, 1] # Column2 → Floor :contentReference[oaicite:11]{index=11}
29
+ output_df["Unit"] = df.iloc[:, 2] # Column3 Unit :contentReference[oaicite:12]{index=12}
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # 5. Clean and map Area (extract number from "507 ft2")
32
+ output_df["Area"] = (
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
+ # 9. (Optional) Leave DeliveryDate & MemoNo. blank or map if available
56
+ # output_df["DeliveryDate"] = ...
57
+ # output_df["MemoNo."] = ...
 
 
 
 
 
58
 
59
+ # 10. Save output with data‑clean‑YYMMDD filename
60
+ suffix = datetime.now().strftime("%y%m%d")
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("## Excel data‑clean Mapping Tool")
68
  with gr.Row():
69
+ inp = gr.File(label="Upload .xls/.xlsx/.xlsm")
70
  btn = gr.Button("Process")
71
  with gr.Row():
72
+ df_view = gr.Dataframe(label="Mapped Data")
73
+ df_download = gr.File(label="Download Output")
74
+ btn.click(process_file, inputs=[inp], outputs=[df_view, df_download])
75
 
76
  demo.launch()