11

I select columns 2 - end from a pandas DataFrame with iloc as

d=c.iloc[:,2:]

now how can I apply a condition to this selection? For example, if column1==1.

Googlebot
  • 15,159
  • 44
  • 133
  • 229

2 Answers2

12

You can use DataFrame.iloc if need filter first column select by position, : means here select all rows:

c[c.iloc[:, 0] == 1]

Sample:

c = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (c)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

df = c[c.iloc[:, 3] == 1]
print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
4  e  5  2  1  2  b
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Why is this accepted as the answer, since it doesn't do what the question asks? In the question we want to keep only columns 2:. – Helen Jun 11 '22 at 10:21
6

This is referred to as mixed indexing in that you want to index by boolean results in rows and position in columns. I'd use loc in order to take advantage of boolean indexing for the rows. But that implies that you need column names values for the column slice.

d.loc[d.column1 == 1, d.columns[2:]]

If your column names are not unique then you can resort to the dreaded chained index.

d.loc[d.column1 == 1].iloc[:, 2:]

What might also be intuitive is to use query afterwards:

d.iloc[:, 2:].query('column1 == 1')
piRSquared
  • 285,575
  • 57
  • 475
  • 624