在熊猫中删除第一列

时间:2019-08-07 20:54:56

标签: python pandas

    command             description
0   aaa ikegroup WORD   Name of the IKE group
1   aaa ikegroup <cr>   
0   aaa locald trace    Show trace data for the locald component(cisco...
0   aaa login trace Show trace data for login sub system
0   aaa password-policy statistics  Show statistics related to password policy
1   aaa password-policy WORD    Name of the Password Policy

以上是我的数据框的外观。 我想摆脱具有数字的第一列,而不是command列。但是,当我这样做时:

df2 = df.drop([df.columns[0]], axis='columns')

这会删除command列,而不是带有索引的列。

如何删除第一个?

2 个答案:

答案 0 :(得分:3)

IIUC,您的第一列就是您的索引。您随时可以使用

将其隐藏在笔记本中
df.style.hide_index()

,但不确定是否能带来很多价值。

如果您要写入文件(例如to_csv),则可以始终将index=False设置为忽略它,例如

df.to_csv('file.csv', index=False)

答案 1 :(得分:1)

您还可以将任何列分配为索引:

df.set_index('command', inplace=True)

其他有用的命令:

# reset the index
df.reset_index(inplace=True, drop=True)

# sort by index
df.sort_index(inplace=True)