[英]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 返回True
或False
,然后使用获得的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.