如何将条件应用于 pandas iloc [英] How to apply a condition to pandas iloc

查看:83
本文介绍了如何将条件应用于 pandas iloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从熊猫数据框中选择 2-结束列,其中 iloc

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

d=c.iloc[:,2:]

现在如何将条件应用于此选择?例如,如果 column1 == 1

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

推荐答案

DataFrame.iloc 如果需要过滤按位置选择第一列,表示此处选择所有行:

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

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

样本

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

这篇关于如何将条件应用于 pandas iloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆