在pandas中删除多个列

13 浏览
0 Comments

在pandas中删除多个列

我正在尝试使用索引号来删除数据集中的多列(即列2和70,分别被索引为1和69),使用以下代码在pandas数据框中进行操作:

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

我得到了以下错误:

TypeError: unhashable type: 'Index'

在我的代码中,[1, 69]被标记并显示如下:

Expected type 'Integral', got 'list[int]' instead

以下代码使用了两行重复的代码来实现我想要的效果(首先删除列索引69,然后删除1,由于删除早期的列会改变后续列的索引,所以顺序很重要)。

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

是否有一种类似于上面第一个代码片段的一行代码的方法来实现这个目的?

0