0

Say I'd like to take the ith column along with every column from the yth column to last column.

Example:

import numpy as np
a = np.random.rand(50).reshape(5,10)

Now, say I'd like to take the 2nd column, along with all columns from #7 through the last.

I can take several slices like so:

a[:,[2,4,8]]

However, what is the correct way to do the following?:

a[:,[1,6:]]

This approach currently results in a syntax error.

yatu
  • 86,083
  • 12
  • 84
  • 139
btobers
  • 175
  • 1
  • 1
  • 7

1 Answers1

0

You can use np.r_ to concatenate multiple multiple slices that can be then applied along the second axis:

a[:, np.r_[1, 6:a.shape[1]]]

Quick check:

np.allclose(a[:, np.r_[1, 6:a.shape[1]]],  a[:, [1,6,7,8,9]])
# True
yatu
  • 86,083
  • 12
  • 84
  • 139