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

如何在Python中移除Pandas DataFrame的索引列名称

解决Pandas DataFrame索引列顶部‘0’的移除问题

你看到的左上角的0是DataFrame的索引名称(index name),可以通过以下几种方式移除:

方法1:直接修改最终DataFrame的索引名称

这是最快捷的方式,处理合并后的moteurs即可:

moteurs.index.name = None

执行后,索引列顶部的0就会消失。

方法2:读取转置时提前处理

在每个文件读取转置后,立即清空索引名称,避免后续concat保留该名称:

dataframe1 = pd.read_csv(chemin_moteurs + '/' + 'moteur1.ppl', comment='#', sep='=', header=None, index_col=0).T
dataframe1.index.name = None

dataframe2 = pd.read_csv(chemin_moteurs + '/' + 'moteur2.ppl', comment='#', sep='=', header=None, index_col=0).T
dataframe2.index.name = None

dataframe3 = pd.read_csv(chemin_moteurs + '/' + 'moteur3.ppl', comment='#', sep='=', header=None, index_col=0).T
dataframe3.index.name = None

dataframe4 = pd.read_csv(chemin_moteurs + '/' + 'moteur4.ppl', comment='#', sep='=', header=None, index_col=0).T
dataframe4.index.name = None

moteurs = pd.concat([dataframe1, dataframe2, dataframe3, dataframe4], ignore_index=True)

方法3:优化文件读取逻辑(推荐)

你的文件是键值对格式(每行键=值),可以直接读取为Series再合并,避免转置带来的索引名问题:

def read_moteur(file_path):
    # 读取单个文件为Series,键作为索引,值为数据
    return pd.read_csv(file_path, comment='#', sep='=', header=None, index_col=0, squeeze=True)

# 读取所有文件并组合成DataFrame
moteurs = pd.DataFrame([
    read_moteur(f"{chemin_moteurs}/moteur1.ppl"),
    read_moteur(f"{chemin_moteurs}/moteur2.ppl"),
    read_moteur(f"{chemin_moteurs}/moteur3.ppl"),
    read_moteur(f"{chemin_moteurs}/moteur4.ppl")
])

这种方式读取的DataFrame天然没有多余的索引名称,逻辑更清晰。

问题原因说明

你之前的读取流程中,header=None让csv的列索引默认是0,index_col=0把第一列设为行索引,转置后原列索引0就变成了新DataFrame的索引名称,concat后这个名称被保留,所以显示在索引列顶部。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 00:45:43