Reputation: 39
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?
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. Right now i m using input = dftrain.loc[:,:'e'] which consider all 4 columns.
Upvotes: 0
Views: 571
Reputation: 402263
Option 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
-
df.iloc[:, 11] = df.iloc[:, idx].sum(axis=1) # sum, for example
Option 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.
Upvotes: 1