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
.
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
.
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
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')