简体   繁体   English

在熊猫中删除第一列

[英]Dropping the first column in 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

Above is how my dataframe looks like. 以上是我的数据框的外观。 I want to get rid of the very first column which has numbers, not the command column. 我想摆脱具有数字的第一列,而不是command列。 However, when I do this: 但是,当我这样做时:

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

This drops the command column, not the ones with the index. 这将删除command列,而不是带有索引的列。

How can I drop the first one? 如何删除第一个?

IIUC, your first column is just your index. IIUC,您的第一列就是您的索引。 You can always hide in your notebook it with 您可以随时将其隐藏在笔记本中

df.style.hide_index()

but not sure if that brings much value. 但不确定是否会带来很多价值。

If you're writing to a file (eg to_csv ), you can always set index=False to ignore it, like 如果要写入文件(例如to_csv ),则可以始终将index=False设置为忽略它,例如

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

You can also assign any column as index: 您还可以将任何列分配为索引:

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

Other useful commands: 其他有用的命令:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM