ShinyaJ commited on
Commit
e423362
·
verified ·
1 Parent(s): 9d48f6a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -172,21 +172,21 @@ def build_cleaned_from_indices(df: pd.DataFrame,
172
  for c in cleaned.columns:
173
  if c not in ("NAME", "ID"):
174
  cleaned[c] = cleaned[c].apply(parse_rank).astype("Int64")
175
- ordered = ["NAME", "ID"] + [c for c in cleaned.columns if c not in ("NAME", "ID")]
176
- cleaned = cleaned[ordered]
177
-
178
- # ✅ เพิ่มการ sort ตาม ID
179
- try:
180
- # ถ้า ID เป็นตัวเลข
181
- cleaned = cleaned.sort_values(by="ID", key=lambda x: pd.to_numeric(x, errors="coerce"))
182
- except Exception:
183
- # ถ้า ID เป็น string ก็จะ sort แบบ lexicographic
184
- cleaned = cleaned.sort_values(by="ID")
185
 
186
- cleaned = cleaned.reset_index(drop=True)
187
  return cleaned
188
 
189
 
 
190
  def max_rank_in(cleaned: pd.DataFrame) -> int:
191
  wards = [w for w in cleaned.columns if w not in ("NAME", "ID")]
192
  mr = 0
 
172
  for c in cleaned.columns:
173
  if c not in ("NAME", "ID"):
174
  cleaned[c] = cleaned[c].apply(parse_rank).astype("Int64")
175
+ # Sort โดยใช้เฉพาะตัวเลขจาก ID
176
+ digits = cleaned["ID"].astype(str).str.extract(r"(\d+)", expand=False)
177
+ num_id = pd.to_numeric(digits, errors="coerce")
178
+
179
+ cleaned = (
180
+ cleaned.assign(_num_id=num_id)
181
+ .sort_values(by="_num_id", kind="mergesort", na_position="last")
182
+ .drop(columns="_num_id")
183
+ .reset_index(drop=True)
184
+ )
185
 
 
186
  return cleaned
187
 
188
 
189
+
190
  def max_rank_in(cleaned: pd.DataFrame) -> int:
191
  wards = [w for w in cleaned.columns if w not in ("NAME", "ID")]
192
  mr = 0