检索除一个指定列之外的所有列的 DataFrame

有没有一种方法可以选择熊猫数据框架对象中的所有列,除了一列?我见过删除专栏的方法,但我不想那么做。

105883 次浏览

use drop method:

df.drop(column_name, axis=1)

you can just select the columns you want without deleting or dropping:

collist = ['col1', 'col2', 'col3']
df1 = df[collist]

Just pass a list of the columns you desire

You can also retrieve the list of columns and then select from that list

collist = df.columns.tolist()
# you can now select from this list any arbritrary range
df1 = df[collist[0:1]]
# or remove a column
collist.remove('col2')
# now select
df1 = df[collist]
# df1 will now only have 'col1' and 'col3'

You could use numpy to build a mask:

import numpy as np
columns = df.columns
mask = np.ones(columns.shape, dtype=bool)
i = 4 #The specified column that you don't want to show
mask[i] = 0
df[columns[mask]]
df.loc[:, df.columns != col]

where col is the name of the column to leave out.

df[ df.columns[df.columns!='not_this_column'] ]

Just as an option, you can select all columns but one (or many) using a list comprehension and df.loc method:

select = [x for x in df.columns if x != "column_you_don't_want"]
df.loc[:, select]

In case you want to leave out more than one column you can try this:

columns_dont_want = ["col1", "col2"]
select = [x for x in df.columns if x not in columns_dont_want]
df.loc[:, select]