Update app.py
Browse files
app.py
CHANGED
|
@@ -355,109 +355,98 @@ with tab1:
|
|
| 355 |
combined_df_list.append(df_kdp)
|
| 356 |
st.success("β KDP.xlsx diproses")
|
| 357 |
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 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 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
else:
|
| 422 |
-
df_final[col] = col_data
|
| 423 |
else:
|
| 424 |
-
df_final[col] =
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
st.
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
| 453 |
except Exception as e:
|
| 454 |
import traceback
|
| 455 |
st.error(f"β Error: {str(e)}")
|
| 456 |
st.code(traceback.format_exc())
|
| 457 |
-
|
| 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:
|