dvsaudit commited on
Commit
e3dc4cf
Β·
verified Β·
1 Parent(s): fdce66c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -97
app.py CHANGED
@@ -355,109 +355,98 @@ with tab1:
355
  combined_df_list.append(df_kdp)
356
  st.success("βœ“ KDP.xlsx diproses")
357
 
358
- # --- PROSES GABUNGAN ---
359
- # 1. Bersihkan setiap dataframe di awal
360
- cleaned_list = []
361
- for name, df in dfs.items():
362
- df = df.loc[:, ~df.columns.duplicated()].copy()
363
- df = df.reset_index(drop=True)
364
- cleaned_list.append(df)
365
-
366
- # 2. Gabungkan (Concat)
367
- combined_df = pd.concat(cleaned_list, ignore_index=True, sort=False)
368
- combined_df = combined_df.reset_index(drop=True)
369
- combined_df = combined_df.loc[:, ~combined_df.columns.duplicated()].copy()
370
-
371
- # 3. Rename & Standarisasi
372
- col_mapping = {
373
- 'ID': 'ID ANGGOTA', 'KELOMPOK': 'KEL',
374
- 'Db Sihara': 'Db HariRaya', 'Cr Sihara': 'Cr HariRaya',
375
- 'Db Hariraya': 'Db HariRaya', 'Cr Hariraya': 'Cr HariRaya',
376
- 'Db Total 2': 'Db Total2', 'Cr Total 2': 'Cr Total2',
377
- 'Db Total Simpanan': 'Db Total', 'Cr Total Simpanan': 'Cr Total',
378
- 'Db Total Pinjaman': 'Db Total2', 'Cr Total Pinjaman': 'Cr Total2'
379
- }
380
- combined_df = combined_df.rename(columns=col_mapping)
381
- # Penting: Buang duplikat SETELAH rename
382
- combined_df = combined_df.loc[:, ~combined_df.columns.duplicated()].copy()
383
-
384
- # 4. Susun Kolom (Metode Rekonstruksi - Anti Reindex Error)
385
- final_df = pd.DataFrame(index=combined_df.index)
386
-
387
- # Tentukan daftar kolom yang diinginkan (Desired + Others)
388
- existing_cols = [c for c in DESIRED_ORDER if c in combined_df.columns]
389
- other_cols = [c for c in combined_df.columns if c not in DESIRED_ORDER]
390
- all_target_cols = list(dict.fromkeys(DESIRED_ORDER + other_cols))
391
-
392
- for col in all_target_cols:
393
- if col in combined_df.columns:
394
- col_data = combined_df[col]
395
- # Jika masih ada duplikat (kemungkinan kecil), ambil kolom pertama
396
- if isinstance(col_data, pd.DataFrame):
397
- final_df[col] = col_data.iloc[:, 0]
398
- else:
399
- final_df[col] = col_data
400
  else:
401
- # Jika kolom tidak ada di file asli, isi 0
402
- final_df[col] = 0
403
-
404
- combined_df = final_df.copy()
405
-
406
- st.divider()
407
- st.subheader("πŸ” Tahap 2: Proses Final (Estimasi & Anomali)")
408
-
409
- # 5. Tambah Estimasi
410
- df_hasil_raw = tambah_kolom_estimasi(combined_df.copy())
411
-
412
- # 6. Susun Kolom Akhir (Metode Rekonstruksi)
413
- final_col_order = list(dict.fromkeys(DESIRED_ORDER + ESTIMASI_COLS))
414
- df_final = pd.DataFrame(index=df_hasil_raw.index)
415
-
416
- for col in final_col_order:
417
- if col in df_hasil_raw.columns:
418
- col_data = df_hasil_raw[col]
419
- if isinstance(col_data, pd.DataFrame):
420
- df_final[col] = col_data.iloc[:, 0]
421
- else:
422
- df_final[col] = col_data
423
  else:
424
- df_final[col] = 0
425
-
426
- df_hasil = df_final.copy()
427
-
428
- # Metrics
429
- col1, col2 = st.columns(2)
430
- with col1:
431
- st.metric("πŸ“Š Total Baris Data", len(df_hasil))
432
- with col2:
433
- anomali_count = df_hasil["Final Filter"].sum()
434
- st.metric("⚠️ Anomali Terdeteksi", int(anomali_count))
435
-
436
- st.divider()
437
- st.write("πŸ“‹ Preview hasil akhir (20 baris pertama):")
438
- st.dataframe(df_hasil.head(20), use_container_width=True)
439
-
440
- # Download
441
- output = io.BytesIO()
442
- with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
443
- df_hasil.to_excel(writer, index=False, sheet_name='THC Hasil')
444
- output.seek(0)
445
-
446
- st.download_button(
447
- label="πŸ“₯ Download Data Lengkap (Estimasi + Final Filter)",
448
- data=output.getvalue(),
449
- file_name="THC_Gabungan_dan_Final_Hasil.xlsx",
450
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
451
- )
452
-
 
 
453
  except Exception as e:
454
  import traceback
455
  st.error(f"❌ Error: {str(e)}")
456
  st.code(traceback.format_exc())
457
- # Debug: Cek kolom duplikat
458
- if 'duplicate labels' in str(e).lower():
459
- st.warning("⚠️ Terdeteksi kolom ganda. Cek daftar kolom ini:")
460
- st.write(combined_df.columns[combined_df.columns.duplicated()].tolist())
461
 
462
  # ── TAB 2: ANALISA SIMPANAN ──────────────────────────────────────────────────
463
  with tab2:
 
355
  combined_df_list.append(df_kdp)
356
  st.success("βœ“ KDP.xlsx diproses")
357
 
358
+ if combined_df_list:
359
+ # 1. Concat semua df
360
+ cleaned_list = []
361
+ for df in combined_df_list:
362
+ df = df.loc[:, ~df.columns.duplicated()].copy()
363
+ df = df.reset_index(drop=True)
364
+ cleaned_list.append(df)
365
+
366
+ combined_df = pd.concat(cleaned_list, ignore_index=True, sort=False)
367
+ combined_df = combined_df.reset_index(drop=True)
368
+ combined_df = combined_df.loc[:, ~combined_df.columns.duplicated()].copy()
369
+
370
+ # 2. Rename & Standarisasi
371
+ col_mapping = {
372
+ 'ID': 'ID ANGGOTA', 'KELOMPOK': 'KEL',
373
+ 'Db Sihara': 'Db HariRaya', 'Cr Sihara': 'Cr HariRaya',
374
+ 'Db Hariraya': 'Db HariRaya', 'Cr Hariraya': 'Cr HariRaya',
375
+ 'Db Total 2': 'Db Total2', 'Cr Total 2': 'Cr Total2',
376
+ 'Db Total Simpanan': 'Db Total', 'Cr Total Simpanan': 'Cr Total',
377
+ 'Db Total Pinjaman': 'Db Total2', 'Cr Total Pinjaman': 'Cr Total2'
378
+ }
379
+ combined_df = combined_df.rename(columns=col_mapping)
380
+ combined_df = combined_df.loc[:, ~combined_df.columns.duplicated()].copy()
381
+
382
+ # 3. Susun Kolom (Anti Reindex Error)
383
+ other_cols = [c for c in combined_df.columns if c not in DESIRED_ORDER]
384
+ all_target_cols = list(dict.fromkeys(DESIRED_ORDER + other_cols))
385
+
386
+ final_df = pd.DataFrame(index=combined_df.index)
387
+ for col in all_target_cols:
388
+ if col in combined_df.columns:
389
+ col_data = combined_df[col]
390
+ if isinstance(col_data, pd.DataFrame):
391
+ final_df[col] = col_data.iloc[:, 0]
 
 
 
 
 
 
 
 
392
  else:
393
+ final_df[col] = col_data
394
+ else:
395
+ final_df[col] = 0
396
+
397
+ combined_df = final_df.copy()
398
+
399
+ st.divider()
400
+ st.subheader("πŸ” Tahap 2: Proses Final (Estimasi & Anomali)")
401
+
402
+ # 4. Tambah Estimasi
403
+ df_hasil_raw = tambah_kolom_estimasi(combined_df.copy())
404
+
405
+ # 5. Susun Kolom Akhir
406
+ final_col_order = list(dict.fromkeys(DESIRED_ORDER + ESTIMASI_COLS))
407
+ df_final = pd.DataFrame(index=df_hasil_raw.index)
408
+ for col in final_col_order:
409
+ if col in df_hasil_raw.columns:
410
+ col_data = df_hasil_raw[col]
411
+ if isinstance(col_data, pd.DataFrame):
412
+ df_final[col] = col_data.iloc[:, 0]
 
 
413
  else:
414
+ df_final[col] = col_data
415
+ else:
416
+ df_final[col] = 0
417
+
418
+ df_hasil = df_final.copy()
419
+
420
+ # Metrics
421
+ col1, col2 = st.columns(2)
422
+ with col1:
423
+ st.metric("πŸ“Š Total Baris Data", len(df_hasil))
424
+ with col2:
425
+ anomali_count = df_hasil["Final Filter"].sum()
426
+ st.metric("⚠️ Anomali Terdeteksi", int(anomali_count))
427
+
428
+ st.divider()
429
+ st.write("πŸ“‹ Preview hasil akhir (20 baris pertama):")
430
+ st.dataframe(df_hasil.head(20), use_container_width=True)
431
+
432
+ # Download
433
+ output = io.BytesIO()
434
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
435
+ df_hasil.to_excel(writer, index=False, sheet_name='THC Hasil')
436
+ output.seek(0)
437
+
438
+ st.download_button(
439
+ label="πŸ“₯ Download Data Lengkap (Estimasi + Final Filter)",
440
+ data=output.getvalue(),
441
+ file_name="THC_Gabungan_dan_Final_Hasil.xlsx",
442
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
443
+ )
444
+
445
  except Exception as e:
446
  import traceback
447
  st.error(f"❌ Error: {str(e)}")
448
  st.code(traceback.format_exc())
449
+
 
 
 
450
 
451
  # ── TAB 2: ANALISA SIMPANAN ──────────────────────────────────────────────────
452
  with tab2: