2

I come from an R background and am having trouble slicing certain sets of columns in pandas.

Let's say I am trying to get the columns with index 0, 3, 5 through 20, and 25th. I would think the way to access what I want is:

df.iloc[:, [0,3,5:20,25]]

but there is a syntax error with the : within the 5:20 line of code. Is there a way to generate a sequence of numbers from 5 through 20 without typing each number out?

Kevin Sun
  • 33
  • 3
  • Hi, this is not efficient but it worked. `result = pd.concat([df.iloc[[0,3]], df.iloc[5:20], df.iloc[[25]]], axis=0)` – talatccan Feb 28 '20 at 23:52
  • Please provide input, output, and desired output samples as per this [link](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) on how to post great questions. Help us help you. – Ukrainian-serge Feb 28 '20 at 23:58

2 Answers2

2

You can use numpy.r_[...]:

df.iloc[:, np.r_[0,3,5:21,25]]

Notice that the slice is 5:21 since numpy uses Python's slicing convention (exclude the upperbound) while pandas includes both the lower and upperbound.

Code Different
  • 90,614
  • 16
  • 144
  • 163
1

You could build the list first and then use .iloc[] with it.

my_list = [0, 3]
my_list.extend(range(5, 21))
my_list.extend([25])
df.iloc[my_list]
jeffhale
  • 3,759
  • 7
  • 40
  • 56