Skip to content

Commit

Permalink
feat: enhance value mapping to support list handling and improve mapp…
Browse files Browse the repository at this point in the history
…ing efficiency
  • Loading branch information
Ovler-Young committed Dec 2, 2024
1 parent 4de3972 commit 65e340b
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/ia_collection_analyzer/streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,35 @@ def transform_data():
elif transform_type == "Numeric Bins":
new_col = pd.qcut(filtered_pd[source_col], num_bins, labels=False)
elif transform_type == "Value Mapping":
new_col = filtered_pd[source_col].copy()
for mapping in st.session_state.mapping_table:
new_col = new_col.replace(mapping["sources"], mapping["target"])
def safe_map(x):
# Convert list to tuple for mapping since lists are unhashable
if isinstance(x, list):
# Check if entire list matches any source
str_val = str(x) # Convert full list to string for matching
if str_val in mapping_dict:
return mapping_dict[str_val]

# Try mapping individual elements
mapped = [mapping_dict.get(item, item) for item in x]
return mapped
else:
return mapping_dict.get(x, x)

# Create mapping dictionary
mapping_dict = {}
for m in st.session_state.mapping_table:
for source in m["sources"]:
# Handle both string representations of lists and regular values
mapping_dict[source] = m["target"]
if source.startswith('[') and source.endswith(']'):
# Also add the actual list/string version
try:
mapping_dict[eval(source)] = m["target"]
except:
pass

# Apply mapping with list handling
new_col = filtered_pd[source_col].map(safe_map)

# Show preview
preview_df = pd.DataFrame(
Expand Down

0 comments on commit 65e340b

Please sign in to comment.