簡體   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

以上是我的數據框的外觀。 我想擺脫具有數字的第一列,而不是command列。 但是,當我這樣做時:

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

這將刪除command列,而不是帶有索引的列。

如何刪除第一個?

IIUC,您的第一列就是您的索引。 您可以隨時將其隱藏在筆記本中

df.style.hide_index()

但不確定是否會帶來很多價值。

如果要寫入文件(例如to_csv ),則可以始終將index=False設置為忽略它,例如

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

您還可以將任何列分配為索引:

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

其他有用的命令:

# 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