You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python数组搜索优化:简化硬件组合至DataFrame的映射实现

Simplifying CPU-GPU-RAM to Family Mapping in Pandas

Hey there! As a fellow Python developer who’s dealt with clunky elif chains for categorical mappings before, let me walk you through a way to clean up that code while keeping your 27-family classification intact.

The Core Idea: Use a Dictionary Mapping

Instead of writing 27 elif statements, we can pre-generate all possible CPU-GPU-RAM combinations and map each directly to its corresponding Family number. This makes your code easier to read, modify, and debug.


Case 1: Your Combination is a Single String Column

If your DataFrame has a column (e.g., 'Hardware_Combination') with values like "HIGH-CPU, MID-GPU, HIGH-RAM", here’s how to set it up:

import itertools
import pandas as pd

# Define the possible levels for each component (match the order from your original elif logic!)
cpu_options = ["LOW", "MID", "HIGH"]
gpu_options = ["LOW", "MID", "HIGH"]
ram_options = ["LOW", "MID", "HIGH"]

# Generate all 27 combinations and build the mapping dictionary
family_map = {}
for family_num, (cpu, gpu, ram) in enumerate(
    itertools.product(cpu_options, gpu_options, ram_options), start=1
):
    # Format the key to exactly match your DataFrame's combination strings
    combo_key = f"{cpu}-CPU, {gpu}-GPU, {ram}-RAM"
    family_map[combo_key] = f"Family {family_num}"

# Apply the mapping to your DataFrame
df["Family"] = df["Hardware_Combination"].map(family_map)

Case 2: Your Components are Separate Columns

If your DataFrame has individual columns (e.g., 'CPU', 'GPU', 'RAM') with values like "HIGH"/"MID"/"LOW", we can use tuples for faster mapping:

import itertools
import pandas as pd

# Same level definitions as before
cpu_options = ["LOW", "MID", "HIGH"]
gpu_options = ["LOW", "MID", "HIGH"]
ram_options = ["LOW", "MID", "HIGH"]

# Build mapping using tuples (more efficient than string keys)
family_map = {}
for family_num, combo_tuple in enumerate(
    itertools.product(cpu_options, gpu_options, ram_options), start=1
):
    family_map[combo_tuple] = f"Family {family_num}"

# Map the tuple of columns to the Family
df["Family"] = pd.Series(list(zip(df["CPU"], df["GPU"], df["RAM"]))).map(family_map)

Key Notes to Keep Your Classification Correct

  • Order Matters: Make sure the order of cpu_options, gpu_options, and ram_options matches the order you used in your original elif chain. This ensures each combination maps to the same Family number as before.
  • Handling Missing Combinations: Since 3*3*3=27, this covers all possible combinations. If your data has unexpected values, map() will return NaN—you can add a fallback with fillna() if needed (e.g., df["Family"] = df["Hardware_Combination"].map(family_map).fillna("Unknown")).

This approach eliminates all redundant elif code, makes your logic explicit, and is way easier to adjust if you ever need to reorder or modify the Family mappings later.

内容的提问来源于stack exchange,提问作者Ruben Ramirez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:36:13