Goo*_*bot 9 python dataframe pandas
我2 - end从 Pandas DataFrame 中选择列iloc作为
d=c.iloc[:,2:]
Run Code Online (Sandbox Code Playgroud)
现在如何将条件应用于此选择?例如,如果column1==1.
您可以使用DataFrame.ilocif 需要过滤第一列按位置选择,:表示这里选择所有行:
c[c.iloc[:, 0] == 1]
Run Code Online (Sandbox Code Playgroud)
样品:
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
Run Code Online (Sandbox Code Playgroud)
这称为混合索引,因为您希望按行中的布尔结果和列中的位置进行索引。我将使用它loc来利用行的布尔索引。但这意味着您需要列切片的列名称值。
d.loc[d.column1 == 1, d.columns[2:]]
Run Code Online (Sandbox Code Playgroud)
如果您的列名不是唯一的,那么您可以诉诸可怕的链式索引。
d.loc[d.column1 == 1].iloc[:, 2:]
Run Code Online (Sandbox Code Playgroud)
query之后使用也可能是直观的:
d.iloc[:, 2:].query('column1 == 1')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18331 次 |
| 最近记录: |