Python 删除特定列时出现轴错误
根据一些分析,我已经确定了我想要选择的特定列作为我的模型的预测值。我已经捕获了这些列编号并将其存储在列表中。我大约有80列,希望循环并删除不在此特定列表中的列。X_train是我想做这件事的专栏。这是我的密码:Python 删除特定列时出现轴错误,python,pandas,dataframe,Python,Pandas,Dataframe,根据一些分析,我已经确定了我想要选择的特定列作为我的模型的预测值。我已经捕获了这些列编号并将其存储在列表中。我大约有80列,希望循环并删除不在此特定列表中的列。X_train是我想做这件事的专栏。这是我的密码: cols_selected = [24, 4, 7, 50, 2, 60, 46, 53, 48, 61] cols_drop = [] for x in range(len(X_train.columns)): if x in cols_selected: p
cols_selected = [24, 4, 7, 50, 2, 60, 46, 53, 48, 61]
cols_drop = []
for x in range(len(X_train.columns)):
if x in cols_selected:
pass
else:
X_train.drop([x])
运行此命令时,我在突出显示代码时遇到以下错误:X_train.drop([X]):
KeyError:“[3]未在axis中找到”
我确信我错过的是一件非常简单的事情。我尝试将inplace=True或axis=1语句与此一起包含,它们都有相同的错误消息(而[]中的值随这些错误代码而更改)
任何帮助都会很好
编辑:以下是使此工作正常的附加内容:
cols_selected = [24, 4, 7, 50, 2, 60, 46, 53, 48, 61]
cols_drop = []
for x in range(len(X_train.columns)):
if x in cols_selected:
pass
else:
cols_drop.append(x)
X_train = X_train.drop(X_train.columns[[cols_drop]], axis=1)
我只是根据问题litle假设: 数据帧示例:
>>> df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
删除特定列B
和C
:
>>> df.drop(['B', 'C'], axis=1)
# df.drop(['B', 'C'], axis=1, inplace=True) <-- to make the change the df itself , use inplace=True
A D
0 0 3
1 4 7
2 8 11
或
我只是根据问题litle假设: 数据帧示例:
>>> df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
删除特定列B
和C
:
>>> df.drop(['B', 'C'], axis=1)
# df.drop(['B', 'C'], axis=1, inplace=True) <-- to make the change the df itself , use inplace=True
A D
0 0 3
1 4 7
2 8 11
或
此外,除了@pygo指出df.drop使用关键字arg来指定轴之外,还可以尝试以下方法:
X_train = X_train[[col for col in X_train.columns if col in cols_selected]]
以下是一个例子:
>>> import numpy as np
>>> import pandas as pd
>>> cols_selected = ['a', 'c', 'e']
>>> X_train = pd.DataFrame(np.random.randint(low=0, high=10, size=(20, 5)), columns=['a', 'b', 'c', 'd', 'e'])
>>> X_train
a b c d e
0 4 0 3 5 9
1 8 8 6 7 2
2 1 0 2 0 2
3 3 8 0 5 9
4 5 9 7 8 0
5 1 9 3 5 9 ...
>>> X_train = X_train[[col for col in X_train.columns if col in cols_selected]]
>>> X_train
a c e
0 4 3 9
1 8 6 2
2 1 2 2
3 3 0 9
4 5 7 0
5 1 3 9 ...
此外,除了@pygo指出df.drop使用关键字arg来指定轴之外,还可以尝试以下方法:
X_train = X_train[[col for col in X_train.columns if col in cols_selected]]
以下是一个例子:
>>> import numpy as np
>>> import pandas as pd
>>> cols_selected = ['a', 'c', 'e']
>>> X_train = pd.DataFrame(np.random.randint(low=0, high=10, size=(20, 5)), columns=['a', 'b', 'c', 'd', 'e'])
>>> X_train
a b c d e
0 4 0 3 5 9
1 8 8 6 7 2
2 1 0 2 0 2
3 3 8 0 5 9
4 5 9 7 8 0
5 1 9 3 5 9 ...
>>> X_train = X_train[[col for col in X_train.columns if col in cols_selected]]
>>> X_train
a c e
0 4 3 9
1 8 6 2
2 1 2 2
3 3 0 9
4 5 7 0
5 1 3 9 ...
根据以下文件: 通过指定标签名称和相应的名称删除行或列 或者直接指定索引名或列名
您不能简单地使用列的索引来删除列。您需要列的名称。此外,
轴
参数必须设置为1
或列
将X\u列下降([X])
替换为X\u列=X\u列下降(X\u列[X],轴列])
,以使示例生效。根据以下文件:
通过指定标签名称和相应的名称删除行或列
或者直接指定索引名或列名
您不能简单地使用列的索引来删除列。您需要列的名称。此外,轴参数必须设置为
1
或列
替换X\u train.drop([X])
为X\u train=X\u train.drop(X\u train.columns[X],axis='columns')
以使示例生效。您所说的数据集不可见,你能给我们看一下DataFrame结构吗?你所说的数据集没有可见性,你能给我们看一下DataFrame结构吗?谢谢你回来。运行了这段代码,但尽管它运行了,但似乎没有删除任何列。每次删除调用后,您需要将结果分配给X_train谢谢您回来。运行了这段代码,但尽管它运行了,但似乎没有删除任何列。每次删除调用后,您需要将结果分配给X_train谢谢您回来。运行了这段代码,但是它返回了一个数据帧,其中有一列和一些我无法识别的值。哦,好的。好吧,如果这对你有帮助的话,我发布了一个快速的例子——你的代码中可能有另一个bug。谢谢你回来。运行了这段代码,但是它返回了一个数据帧,其中有一列和一些我无法识别的值。哦,好的。好的,我发布了一个快速的例子,如果这对您有帮助的话——您的代码中可能还有另一个bug。