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

Pandas将分类值转换为数值并生成新列(非LabelEncode)

Solution for Adding Numeric Columns from Ordered Categorical Strings in Pandas

Hey there! Let's walk through this step by step—since you're new to Python and pandas, I'll keep things clear and avoid overly complex code. Here's how you can generate new _scl columns without modifying your original data:

Step 1: Identify Columns with Ordered Categorical Values

First, we need to pick out which columns have the ordered string/categorical values you want to convert. There are two common scenarios here:

  • Your columns are already marked as ordered categorical in pandas
  • They're stored as object (string) type but represent an ordered set (like "low" → "medium" → "high")

Code to Filter Columns

import pandas as pd

# Assume your original DataFrame is named `df`
# Option 1: Auto-detect existing ordered categorical columns
ordered_cat_cols = [
    col for col in df.select_dtypes(include='category').columns
    if df[col].cat.ordered
]

# Option 2: Manually specify columns if you know which ones are ordered
# (Use this if your columns are still string/object type)
# ordered_cat_cols = ["your_column_name_1", "your_column_name_2"]

Step 2: Generate New _scl Columns with Numeric Values

Now we'll loop through each identified column and create a new column (with _scl suffix) that holds the numeric equivalent of the ordered values. We'll make sure not to modify the original columns.

Code for Conversion

# If you want to keep your original DataFrame untouched, make a copy first
new_df = df.copy()

# Loop through each ordered column
for col in ordered_cat_cols:
    if new_df[col].dtype == 'category':
        # For existing ordered categories, use .cat.codes to get numeric values
        new_df[f"{col}_scl"] = new_df[col].cat.codes
    else:
        # For string/object columns, define your ordered mapping first
        # Replace this with your actual value-to-number mapping
        order_mapping = {
            "low": 0,
            "medium": 1,
            "high": 2
        }
        # Map the strings to numbers and create the new column
        new_df[f"{col}_scl"] = new_df[col].map(order_mapping)
        
        # Alternative (more robust): Convert to ordered category first, then get codes
        # new_df[f"{col}_scl"] = pd.Categorical(
        #     new_df[col],
        #     categories=["low", "medium", "high"],  # Match your order
        #     ordered=True
        # ).codes

Step 3: Verify Your New Columns

Check that the new _scl columns were created correctly:

# View the first few rows of all _scl columns
print(new_df.filter(like='_scl').head())

Key Notes for You

  • Adjust the mapping: The order_mapping dictionary needs to match your actual string values and their desired numeric order. For example, if a column uses "beginner" → "intermediate" → "expert", update the mapping accordingly.
  • 81 columns isn't a problem: The loop will handle as many ordered columns as you identify—no need to write code for each one individually.
  • Original data stays intact: By working on new_df (a copy of your original DataFrame), your original columns won't be modified at all.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:21:07