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

如何将表格中Column A的多行值转换为多列并匹配Column B数据

数据格式转换方案

原始数据

Column AColumn B
Apple2
Bat2
Cat4
Bat2.5
Apple6
Cat4.8

目标格式

AppleBatCat
224
62.54.8

实现方法(Python pandas)

  1. 先安装pandas:
pip install pandas
  1. 执行转换代码:
import pandas as pd

# 构造原始数据(实际场景可通过pd.read_csv/pd.read_excel读取本地文件)
raw_data = pd.DataFrame({
    'Column A': ['Apple', 'Bat', 'Cat', 'Bat', 'Apple', 'Cat'],
    'Column B': [2, 2, 4, 2.5, 6, 4.8]
})

# 给每个类别添加行序号,确保对应值能正确分行
raw_data['row_idx'] = raw_data.groupby('Column A').cumcount()

# 转换为宽表格式
wide_data = raw_data.pivot(index='row_idx', columns='Column A', values='Column B').reset_index(drop=True)

# 查看转换结果
print(wide_data)

运行后即可得到目标格式的表格,若需要保存为文件,可添加wide_data.to_excel('result.xlsx', index=False)或wide_data.to_csv('result.csv', index=False)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 22:25:17