I would like to select from a pandas dataframe specific columns using column index.
In particular, I would like to select columns index by the column index generate
The equivalent is numpy's r_. It combines integer slices without needing to call ranges for each of them:
np.r_[2:4, 7:11, 21:25]
Out: array([ 2, 3, 7, 8, 9, 10, 21, 22, 23, 24])
df = pd.DataFrame(np.random.randn(1000))
df.iloc[np.r_[2:4, 7:11, 21:25]]
Out:
0
2 2.720383
3 0.656391
7 -0.581855
8 0.047612
9 1.416250
10 0.206395
21 -1.519904
22 0.681153
23 -1.208401
24 -0.358545