[英]Slice multiple column ranges with Pandas
Suppose I have 20 Columns in a data set and i want to use 19 as an input. 假设我在数据集中有20列,并且我想使用19列作为输入。 and input columns are columns from 1:10 and 12: 20. and I want to use 11th column as an output.
输入列是从1:10到12:20的列。我想使用第11列作为输出。 so how to give this kind of range using pandas?
那么如何使用熊猫给这种距离呢?
for example: Example Data Set 例如: 示例数据集
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. 考虑上述数据,它有4列,但我只需要输入3列,但这些列是b,d,e,而我想跳过c列。 Right now im using input = dftrain.loc[:, :'e' ] which consider all 4 columns.
现在,im使用input = dftrain.loc [:, :'e' ]来考虑所有4列。
Option 1 选项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])
Pass this to iloc
- 将此传递给
iloc
df.iloc[:, 11] = df.iloc[:, idx].sum(axis=1) # sum, for example
Option 2 选项2
pd.IndexSlice
idx = pd.IndexSlice[0:11, 12:20]
idx
(slice(0, 11, None), slice(12, 20, None))
You can use idx
in the same manner as before. 您可以像以前一样使用
idx
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.