简体   繁体   中英

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. However, when I do this:

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

This drops the command column, not the ones with the index.

How can I drop the first one?

IIUC, your first column is just your index. 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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