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 generated by c(12:26,69:85,96:99,134:928,933:935,940:967)
in R. I wonder how can I do that in Python?
I am thinking something like the following, but of course, python does not have a function called c()...
input2 = input2.iloc[:,c(12:26,69:85,96:99,134:928,933:935,940:967)]
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
Putting @hrbrmstr 's comment into an answer, because it solved my issue and I want to make it clear that this question is resolved. In addition, please note that range(a,b) gives the numbers (a, a+1, ..., b-2, b-1), and doesn't include b.
R's combine function
c(4,12:26,69:85,96:99,134:928,933:935)
is translated into Python as
[4] + list(range(12,27)) + list(range(69,86)) + list(range(96,100)) + list(range(134,929)) + list(range(933,936))
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