问题描述
假设我在一个数据集中有20列,我想使用19列作为输入.输入列是从1:10到12:20的列.我想使用第11列作为输出.那么如何使用熊猫给这种距离呢?
Suppose I have 20 Columns in a data set and i want to use 19 as an input. and input columns are columns from 1:10 and 12: 20. and I want to use 11th column as an output. so how to give this kind of range using pandas?
例如:示例数据集
考虑上面的数据,它有4列,但是我只需要输入3列,但是那些列是b,d,e,我想跳过c列.现在我正在使用输入= dftrain.loc [:,:'e']考虑所有4列.
consider above data it have 4 columns but i have to take input only 3 columns but those columns are b,d,e and i want to skip c column. Right now i m usinginput = dftrain.loc[:,:'e']which consider all 4 columns.
推荐答案
选项1 np.r_
idx = np.r_[0:11, 12:20]
idx
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17,
18, 19])
将此内容传递给iloc
-
df.iloc[:, 11] = df.iloc[:, idx].sum(axis=1) # sum, for example
选项2 pd.IndexSlice
Option 2pd.IndexSlice
idx = pd.IndexSlice[0:11, 12:20]
idx
(slice(0, 11, None), slice(12, 20, None))
您可以以与以前相同的方式使用idx
.
You can use idx
in the same manner as before.
这篇关于用Pandas分割多个列范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!