按索引删除多个 Pandas 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51765983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:54:41  来源:igfitidea点击:

Dropping multiple Pandas columns by Index

pythonpandasnumpydataframeindexing

提问by BAC83

I have a large pandas dataframe (>100 columns). I need to drop various sets of columns and i'm hoping there is a way of using the old

我有一个大Pandas数据框(> 100 列)。我需要删除各种列,我希望有一种方法可以使用旧的

df.drop(df.columns['slices'],axis=1)

I've built selections such as:

我已经建立了选择,例如:

a = df.columns[3:23]
b = df.colums[-6:]

as aand brepresent column sets I want to drop.

作为ab代表我想删除的列集。

The following

下列

list(df)[3:23]+list(df)[-6:]

yields the correct selection, but i can't implement it with a drop:

产生正确的选择,但我不能用drop

df.drop(df.columns[list(df)[3:23]+list(df)[-6:]],axis=1)

ValueError: operands could not be broadcast together with shapes (20,) (6,)

ValueError:操作数无法与形状一起广播 (20,) (6,)

I looked around but can't get my answer.

我环顾四周,但无法得到我的答案。

Selecting last n columns and excluding last n columns in dataframe

选择最后 n 列并排除数据框中的最后 n 列

(Below pertains to the error I receive):

(以下与我收到的错误有关):

python numpy ValueError: operands could not be broadcast together with shapes

python numpy ValueError:操作数无法与形状一起广播

This one feels like they're having a similar issue, but the 'slices' aren't separate: Deleting multiple columns based on column names in Pandas

这个感觉就像他们遇到了类似的问题,但“切片”不是分开的: 根据 Pandas 中的列名删除多个列

Cheers

干杯

回答by jpp

You can use np.r_to seamlessly combine multiple ranges / slices:

您可以使用np.r_无缝组合多个范围/切片:

from string import ascii_uppercase

df = pd.DataFrame(columns=list(ascii_uppercase))

idx = np.r_[3:10, -5:0]

print(idx)

array([ 3,  4,  5,  6,  7,  8,  9, -5, -4, -3, -2, -1])

You can then use idxto index your columns and feed to pd.DataFrame.drop:

然后,您可以使用idx索引您的列并提供给pd.DataFrame.drop

df.drop(df.columns[idx], axis=1, inplace=True)

print(df.columns)

Index(['A', 'B', 'C', 'K', 'L', 'M', 'N',
       'O','P', 'Q', 'R', 'S', 'T', 'U'], dtype='object')

回答by DINA TAKLIT

You can use this simple solution:

您可以使用这个简单的解决方案:

cols = [3,7,10,12,14,16,18,20,22]
df.drop(df.columns[cols],axis=1,inplace=True)

the result :

结果 :

    0   1   2   4   5   6   8   9    11  13      15     17      19       21
0   3   12  10  3   2   1   7   512  64  1024.0  -1.0   -1.0    -1.0    -1.0
1   5   12  10  3   2   1   7   16   2   32.0    32.0   1024.0  -1.0    -1.0
2   5   12  10  3   2   1   7   512  2   32.0    32.0   32.0    -1.0    -1.0
3   5   12  10  3   2   1   7   16   1   32.0    64.0   1024.0  -1.0    -1.0

As you can see the columns with given index have been all deleted.

如您所见,具有给定索引的列已全部删除。

You can replace the int value by the name of the column you have in your array if we suppose you have A,B,C ...etc you can replace int values in colslike this for example :

如果我们假设您有 A、B、C ...等,您可以用数组中的列名替换 int 值,您可以cols像这样替换 int 值,例如:

cols = ['A','B','C','F']

回答by Chabu

This returns the dataframe with the columns removed

这将返回删除列的数据框

df.drop(list(df)[2:5], axis=1)

回答by shivsn

IIUC:

IUC:

a = df.columns[3:23].values.tolist()
b = df.colums[-6:].values.tolist()

a.extend(b)
df.drop(a,1,inplace=True)

回答by Patrick Maynard

I have run into a similar issue before and had trouble with this but fixed it by "subtracting" one df from the other, not sure if this will work for you but it did for me:

我之前遇到过类似的问题并且遇到了问题,但是通过从另一个“减去”一个 df 来修复它,不确定这是否适合你,但它对我有用:

df = df[~df.index.isin(a.index)]
df = df[~df.index.isin(b.index)]