JoyceD
JoyceD

Reputation: 37

Can I use index position and also index slice in iloc function at the same time?

Can I use: iloc[:, [1,2,3,27, 4:27]

I want to reorder column by column index and include all column in output

Upvotes: 2

Views: 95

Answers (1)

Chrysophylaxs
Chrysophylaxs

Reputation: 6583

Yes, you can, but you'll need to build a list of indices without the slice. Numpy has a nifty helper for this: np.r_:

>>> np.r_[1, 2, 3, 27, 4:27]
array([ 1,  2,  3, 27,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26])

so your code becomes:

df.iloc[:, np.r_[1, 2, 3, 27, 4:27]]

Upvotes: 7

Related Questions