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

如何在显示pandas DataFrame时移除左侧索引列以获得预期输出

移除pandas DataFrame展示时左侧索引列的方法

你之前使用的reset_index(drop=True)作用是重置DataFrame的内部索引值、丢弃原有索引列,不会控制展示阶段是否渲染索引列,因此无法实现你要的效果,可按使用场景选择对应方案:

场景1:Jupyter/Notebook 环境使用display展示

使用pandas内置的Styler样式API隐藏索引:

import pandas as pd 

d = {'indexes': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
# 隐藏索引后展示
display(df.style.hide(axis='index'))

如果你的pandas版本低于1.4.0,改用以下写法:

display(df.style.hide_index())

场景2:控制台直接打印输出

使用to_string方法关闭索引输出再打印:

import pandas as pd 

d = {'indexes': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
print(df.to_string(index=False))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 18:15:03