2

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.

Helen
  • 316
  • 3
  • 16

1 Answers1

3

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])
mozway
  • 194,879
  • 13
  • 39
  • 75
  • 1
    Let me add that instead of "to_numpy()", for earlier pandas versions it will be "values". As per the relevant answer here: https://stackoverflow.com/questions/54424818/10-minutes-to-pandas-tutorial-to-numpy-does-not-exist – Helen Jun 11 '22 at 08:54