首页 > 解决方案 > 删除数据框 Pn 中添加的默认列

问题描述

我的 DataFrame 上有这个当前输出:

     x_1   x_2     x_3     x_4        x_5    ID
0     159   xyz  883nne2  28h93  xx_lightz    10
1     159   xyz  883nne2  28h93  xx_lightz    10

我一直在研究,找不到创建 df 时默认生成的最左侧列的名称(没有列名)。这个名字是什么,它是如何被丢弃的?

预期结果应该是:

x_1   x_2     x_3     x_4        x_5    ID
159   xyz  883nne2  28h93  xx_lightz    10
159   xyz  883nne2  28h93  xx_lightz    10

标签: python-3.xpandasdataframe

解决方案


原因是index,而不是列,让我们检查一下

df.columns
Out[100]: Index(['x_1', 'x_2', 'x_3', 'x_4', 'x_5', 'ID'], dtype='object')

如果只需要print

print(df.to_string(index=False))
 x_1  x_2      x_3    x_4        x_5  ID
 159  xyz  883nne2  28h93  xx_lightz  10
 159  xyz  883nne2  28h93  xx_lightz  10

推荐阅读