使用&比较跨Pandas中多个列的布尔行值. /np.where()/np.any() [英] Compare Boolean Row values across multiple Columns in Pandas using & / np.where() / np.any()

查看:149
本文介绍了使用&比较跨Pandas中多个列的布尔行值. /np.where()/np.any()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框:

I have a dataframe that looks like:

   a A  a B  a C  a D  a E  a F  p A  p B  p C  p D  p E  p F
0    0    0    0    0    0    0    0    0    0    0    0    0
1    1    0    0    0    0    0    0    0    0    0    0    0
2    0    1    0    0    0    0    0    0    1    0    0    0
3    0    0    1    0    0    1    0    0    0    0    0    0
4    0    0    0    1    0    1    0    0    0    0    0    0
5    0    0    0    0    1    0    0    0    0    0    0    0
6    0    0    0    0    0    0    1    0    0    0    0    0

df = pd.DataFrame({'p A':[0,0,0,0,0,0,1],'p B':[0,0,0,0,0,0,0],'p C':[0,0,1,0,0,0,0],'p D':[0,0,0,0,0,0,0],'p E':[0,0,0,0,0,0,0],'p F':[0,0,0,0,0,0,0],'a A':[0,1,0,0,0,0,0],'a B':[0,0,1,0,0,0,0],'a C':[0,0,0,1,0,0,0],'a D':[0,0,0,0,1,0,0],'a E':[0,0,0,0,0,1,0],'a F': [0,0,0,1,1,0,0]})

注意:这是我的实际数据的简化版本.

Note: This is a much simplified version of my actual data.

a代表实际; p代表预测的; A-F代表一系列标签

a stands for Actual; p stands for Predicted; A - F represent a series of labels

我想编写一个查询,对于以下情况,我的数据帧中的每一行都返回True :("p列"中的所有行值= 0)和("a列"中的至少一个行值= 1),即对于每一行,p列固定为0,至少1列固定为1.

I want to write a query that, for each row in my dataframe, returns True when: (all row values in "p columns" = 0 ) and (at least one row value in "a columns" = 1) i.e. for each row, p columns are fixed at 0 and at least 1 a column = 1.

使用对 Pandas数据框查找所有列均相等的行的答案

使用熊猫比较两列 我目前通过使用&np.any()

Using answers to Pandas Dataframe Find Rows Where all Columns Equal and Compare two columns using pandas I achieve this currently by using & and np.any()

((df.iloc[:,6] == 0) & (df.iloc[:,7] == 0) & (df.iloc[:,8] == 0) & (df.iloc[:,9] == 0) & (df.iloc[:,10] == 0) & (df.iloc[:,11] == 0) & df.iloc[:,0:6].any(axis = 1) )

>>
0    False
1     True
2    False
3     True
4     True
5     True
6    False
dtype: bool

有没有更简洁易懂的方式可以实现这一目标?

Is there a more succinct, readable way I can achieve this?

推荐答案

您可以将~用于具有

You can use ~ for invert boolean mask with iloc for select by position:

print (~df.iloc[:,6:11].any(1) & df.iloc[:,0:6].any(1))
0    False
1     True
2    False
3     True
4     True
5     True
6    False
dtype: bool

或使用 filter 要按列名进行选择,请 any 检查至少一个True 检查是否所有值都是每行True.

Or use filter for select by column names, any for check at least one True or all for check if all values are True per row.

函数 eq 是与0进行比较.

print (~df.filter(like='p').any(1) & df.filter(like='a').any(1))
0    False
1     True
2    False
3     True
4     True
5     True
6    False
dtype: bool


print (df.filter(like='p').eq(0).all(1) & df.filter(like='a').any(1))
0    False
1     True
2    False
3     True
4     True
5     True
6    False
dtype: bool

这篇关于使用&比较跨Pandas中多个列的布尔行值. /np.where()/np.any()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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