1
votes

I am an R user who is currently learning Python and I am trying to replicate a method of selecting columns used in R into Python.

In R, I could select multiple columns like so:

df[,c(2,4:10)]

In Python, I know how iloc works, but I couldn't split between a single column number and a consecutive set of them.

This wouldn't work

df.iloc[:,[1,3:10]]

So, I'll have to drop the second column like so:

df.iloc[:,1:10].drop(df.iloc[:,1:10].columns[1] , axis=1)

Is there a more efficient way of replicating the method from R in Python?

2
df.iloc[:,[1]+range(3,10)] - DJK

2 Answers

5
votes

You can use np.r_ that accepts mixed slice notation and scalar indices and concatenate them as 1-d array:

import numpy as np
df.iloc[:,np.r_[1, 3:10]]

df = pd.DataFrame([[1,2,3,4,5,6]])

df

#   0   1   2   3   4   5
#0  1   2   3   4   5   6

df.iloc[:, np.r_[1, 3:6]]

#   1   3   4   5
#0  2   4   5   6

As np.r_ produces:

np.r_[1, 3:6]
# array([1, 3, 4, 5])
0
votes

Assuming one wants to select multiple columns of a DataFrame by their name, considering the Dataframe df

df = pandas.DataFrame({'A' : ['X', 'Y'], 
                        'B' : 1, 
                        'C' : [2, 3]})

Considering one wants the columns A and C, simply use

df[['A', 'C']]
>>>   A  C
   0  X  2
   1  Y  3 

Note that if one wants to use it later on one should assign it to a variable.