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

如何获取pandas Series的索引与对应值并导出为Excel分列存储

实现方案

你操作的对象是pandas Series类型,不是普通Python列表,按位置取值默认只会返回对应值,不会返回索引。

1. 打印首行索引+对应值

# 假设你的Series对象命名为s
print(f"{s.index[0]}   {s.iloc[0]}")

执行后直接输出A 22的目标效果。

2. 写入Excel,A、WO作为独立列

先将Series转换为行格式的DataFrame,再写入Excel即可:

import pandas as pd

# 转换为单行DataFrame,原Series索引自动转为列名
df = s.to_frame().T
# 写入Excel,设置index=False去掉冗余的行号列
df.to_excel("result.xlsx", index=False)

写入完成后Excel的表头为A、WO,第一行对应值为22、8。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 14:24:03