Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of R c() function, for dataframe column indices?

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)]
like image 821
user5309995 Avatar asked Dec 15 '22 12:12

user5309995


2 Answers

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
like image 124
ayhan Avatar answered Jan 17 '23 13:01

ayhan


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))
like image 38
tshynik Avatar answered Jan 17 '23 15:01

tshynik