Home > Back-end >  Selecting multiple columns, both consecutive and non-consecutive, in a Pandas dataframe
Selecting multiple columns, both consecutive and non-consecutive, in a Pandas dataframe

Time:06-11

I am trying to run the following:

X = d.iloc[:, [13, 30, 35:45]].values

It fails at the range 35:45.

PS: There is this question with many useful answers, but they don't address the issue of getting both consecutive and non-consecutive columns.

CodePudding user response:

Use np.r_:

import numpy as np
X = d.iloc[:, np.r_[13, 30, 35:45]].to_numpy()

Intermediate output of np.r_[13, 30, 35:45]:

array([13, 30, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44])
  • Related