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

如何基于另一列的值编辑Pandas DataFrame中的指定列

Pandas批量修改列值:按规则插入指定内容

原始数据

Column to editValue to insertRule
protein.carbs.fatsbananahealthy
protein.carbs.fatschipsunhealthy

需求说明

根据Rule列的规则批量修改Column to edit列:

  • 规则为healthy时,在carbs与fats之间插入Value to insert的值
  • 规则为unhealthy时,将原carbs和fats替换为needHealthy,并在中间插入Value to insert的值
    要求用Pandas原生高效方法处理,适配万级以上数据量。

解决方案

使用向量化字符串拆分+np.where条件判断实现,全程避免逐行循环,保证处理效率:

代码实现

import pandas as pd
import numpy as np

# 构造原始DataFrame
df = pd.DataFrame({
    'Column to edit': ['protein.carbs.fats', 'protein.carbs.fats'],
    'Value to insert': ['banana', 'chips'],
    'Rule': ['healthy', 'unhealthy']
})

# 按分隔符拆分目标列,得到各部分内容
split_parts = df['Column to edit'].str.split('.', expand=True)

# 根据规则批量生成新列值
df['Column to edit'] = np.where(
    df['Rule'] == 'healthy',
    # healthy规则:拼接原前两段 + 插入值 + 原第三段
    split_parts[0] + '.' + split_parts[1] + '.' + df['Value to insert'] + '.' + split_parts[2],
    # unhealthy规则:拼接原第一段 + needHealthy + 插入值 + needHealthy
    split_parts[0] + '.needHealthy.' + df['Value to insert'] + '.needHealthy'
)

print(df)

输出结果

Column to editValue to insertRule
protein.carbs.banana.fatsbananahealthy
protein.needHealthy.chips.needHealthychipsunhealthy

核心优势

  1. 向量化操作:str.split和np.where都是Pandas/NumPy的向量化方法,比apply逐行处理效率高数倍,适配万级以上数据
  2. 逻辑清晰:拆分后直接按规则拼接字符串,避免复杂的正则或循环逻辑
  3. 可扩展性:如果后续新增规则,可改用np.select实现多条件分支,依然保持向量化性能

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 21:40:53