leadingbridge commited on
Commit
07f4651
·
verified ·
1 Parent(s): dfdfa21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -48
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/.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] # Column1Usage :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()
 
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 1Address
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()