上一个和下一个数组应该有两个很常见的值pandas

编程入门 行业动态 更新时间:2024-10-25 10:27:12
本文介绍了上一个和下一个数组应该有两个很常见的值pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数据框,

Test Test1 [1,1,1] [1,2,2] [1,2,2] [1,0,1] [1,0,1] [1,1,0] [2,2,0] [0,2,2] [1,2,0] [1,0,2]

我正在尝试比较两个数字应该匹配并且第三个数字应该为o的两个数组,所以像[1,0,1] [1,1,0]应该被匹配并且当两个数字匹配并且第三个数字是0时返回true. [2,2,0] [0,2,2]相同,但[1,2,0] [1,0,2]不应匹配,因为它没有相同的数字.将返回false.那么,有什么办法吗?

I am trying to compare the two arrays where 2 numbers should match and third one should be o.So like [1,0,1] [1,1,0] should be matched and return true as two numbers are matching and third one is 0 . same for [2,2,0] [0,2,2] but [1,2,0] [1,0,2] this should not match as it does not have the same numbers.will return false. So,Is there any way to do this ?

推荐答案

这是您需要的测试功能(我认为,假设所有值均为正值)

This is the testing function you need (I think, assuming all values are positive)

def test_func(x): A = x[0] B = x[1] f = lambda X: np.unique(X, return_counts = True) Au, Ac = f(A) Bu, Bc = f(B) return np.all(Au == Bu) and \ Au.size == 2 and \ Ac[0] == 1 and \ Bc[0] == 1

并要在pandas中申请(不是熊猫专家,但我认为这应该可行):

And to apply in in pandas (not a pandas expert, but I think this should work):

df['new_col'] = df[['Test', 'Test1']].apply(test_func, axis = 1)

在任何情况下,此问题应该可以帮助您将函数应用于两列.

in any case, this question should help you apply your function over two columns.

更多推荐

上一个和下一个数组应该有两个很常见的值pandas

本文发布于:2023-11-16 06:26:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1601811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   常见   有两个   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!