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

如何在Pandas中实现类似Spotfire的透视:拼接列生成新列并保留原列

解决Pandas透视时保留标识列并自定义列名的问题

问题分析

你当前的pivot代码仅将x、y设为索引,导致lot_id、run、wafer这些唯一标识列丢失。要实现Spotfire的透视效果,需要:

  1. 将所有需保留的标识列纳入透视索引
  2. 把test_number与test_name拼接成新的列名

实现步骤

  1. 创建拼接列:先把test_number转为字符串,再和test_name拼接成新列(格式如"11.Test2")
fromCSV['test_col'] = fromCSV['test_number'].astype(str) + '.' + fromCSV['test_name']
  1. 执行透视并还原索引列:将lot_id、run、wafer、x、y全部设为透视索引,用拼接后的test_col作为列名,最后通过reset_index()把索引转回普通列
pivoted_df = fromCSV.pivot(
    index=['lot_id', 'run', 'wafer', 'x', 'y'],
    columns='test_col',
    values='value'
).reset_index()

结果说明

执行后得到的pivoted_df会以lot_id、run、wafer、x、y作为首列,后续列名均为test_number.test_name的格式,对应位置填充原value值,完全匹配你需要的目标结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:23:22