The best way to do this in pandas is to use drop
:
(在熊猫中做到这一点的最好方法是使用drop
:)
df = df.drop('column_name', 1)
where 1
is the axis number ( 0
for rows and 1
for columns.)
(其中1
是轴编号( 0
代表行, 1
代表列)。)
To delete the column without having to reassign df
you can do:
(要删除该列而不必重新分配df
您可以执行以下操作:)
df.drop('column_name', axis=1, inplace=True)
Finally, to drop by column number instead of by column label , try this to delete, eg the 1st, 2nd and 4th columns:
(最后,要按列号而不是按列标签删除,请尝试将其删除,例如第一,第二和第四列:)
df = df.drop(df.columns[[0, 1, 3]], axis=1) # df.columns is zero-based pd.Index
Also working with "text" syntax for the columns:
(还可以对列使用“文本”语法:)
df.drop(['column_nameA', 'column_nameB'], axis=1, inplace=True)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…