本文介绍了上一个和下一个数组应该有两个很常见的值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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:45