首页 > 解决方案 > 删除熊猫数据框上的索引和列

问题描述

我有这个清单:

import pandas as pd
l = [[1,2,3],[4,5,6],[7,8,9]]
New_dataframe = pd.DataFrame(l)
print(New_dataframe)

输出:

   0  1  2
0  1  2  3
1  4  5  6
2  7  8  9

我想删除那些索引的行和列。如何实现?DataFrame 我想看看是这样的:

1  2  3
4  5  6
7  8  9

如何删除该索引列和行?

标签: pythonpandas

解决方案


如果只想看到值可以转换为2d numpy array

print (New_dataframe.values)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

如果需要DataFrame,可以通过以下方式查看:

print (New_dataframe.to_csv(index=False, header=None, sep=' '))
1 2 3
4 5 6
7 8 9

print (New_dataframe.to_string(index=False, header=None))
1  2  3
4  5  6
7  8  9

编辑:

要转换为没有索引和标题的 excel,请使用参数index=Falseheader=None

New_dataframe.to_excel('test.xlsx', index=False, header=None)

推荐阅读