TIF_E41253310/scripts/tests/test_find_column.py

34 lines
1.1 KiB
Python

import pandas as pd
import re
df = pd.read_csv('Data_2024_Tracer_study.csv', sep=';', encoding='utf-8-sig')
cols = list(df.columns)
def normalize(col_name):
if pd.isna(col_name):
return ""
return str(col_name).strip().lower()
def find_column(source_cols, possible_names):
source_cols_norm = [(normalize(c), c) for c in source_cols]
for name in possible_names:
name_norm = normalize(name)
# Exact match
for norm_col, orig_col in source_cols_norm:
if norm_col == name_norm:
return orig_col
# Partial match - cari kode di dalam string
kode_match = re.search(r'[fF]\d+[a-zA-Z0-9]*', name)
if kode_match:
kode = kode_match.group(0).lower()
for norm_col, orig_col in source_cols_norm:
if kode in norm_col:
return orig_col
return None
# Test
tests = ['F8', 'F502', 'F1761', 'F401', 'F1001', 'f8', 'f502', 'F5a1', 'F1102']
for t in tests:
found = find_column(cols, [t])
print(f"find_column(['{t}']) = {repr(found)}")