繁体   English   中英

如何找到三列中的数字超过 pandas 中其他列值的一半

[英]How to find the numbers in three columns which are more than half the values of other column in pandas

X、Y 和 Z 三列具有随机值。

X、Y 和 Z 值不应小于彼此值的一半。

在数学逻辑方面需要帮助

  X    Y     Z
5461  919  8269
5400  5000 7000  <---
4000   1     35

output 应为 5400 5000 7000。在第一行,Y 小于 X 和 Z 的一半。在第三行,Y 和 Z 都小于 X 的一半。

逻辑很简单:只要检查每个值是否小于其他两个值的一半。 使用 apply 返回TrueFalse ,然后使用获得的boolean值过滤行。

import pandas as pd
a = pd.DataFrame([[
5461 , 919  ,8269],
[5400 , 5000, 7000], 
[4000 ,  1  ,   35]], columns = ['X','Y','Z'])
print(a)

def less(a,b,c):
    if a<b/2 or a<c/2:
        return True

def cond(a,b,c):
    if less(a,b,c) or less(b,a,c) or less(c,a,b):
        return False
    return True

a[a.apply(lambda x: cond(x['X'],x['Y'],x['Z']), axis=1)]

Output

      X     Y     Z
1  5400  5000  7000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM