I have yet to find an answer for this anywhere. I am attempting to select columns number 2
and 86:100
. Obviously, I would rather not select them by label.
Intuitively I have tried: df_new = df.iloc[:,[2,86:100]]
to no avail.
What is the most efficient way of selecting these columns?
You can use np.r_
to combine slices:
df = pd.DataFrame(np.random.random((3, 10)))
res = df.iloc[:, np.r_[2, 5:10]]
print(res)
2 5 6 7 8 9
0 0.489923 0.406723 0.085721 0.235617 0.724768 0.398237
1 0.697457 0.565602 0.177975 0.215762 0.377650 0.658344
2 0.116625 0.770128 0.930788 0.367666 0.044933 0.486751
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With