我有一个数据框,如,
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。那么,有什么办法可以做到这一点?
这是您需要的测试功能(我认为,假设所有值均为正)
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
中申请(不是熊猫专家,但我认为这应该可行):
df['new_col'] = df[['Test', 'Test1']].apply(test_func, axis = 1)
无论如何,this question应该可以帮助您将函数应用于两列。
你的意思是这样吗?
>>> def compare_tuples(x, y):
... return (
... # there is any series of size two which is common in both tuples
... any(x[i:i+2] == y[j:j+2] for i in range(2) for j in range(2))
... # there is at least one zero at third position
... and 0 in (x[2], y[2])
... )
>>> df = pd.DataFrame.from_records([
... [(1, 1, 1), (1, 2, 2)], # False
... [(1, 2, 2), (1, 0, 1)], # False
... [(1, 0, 1), (1, 1, 0)], # True - (1, 0) matches and 0 in third position
... [(2, 2, 0), (0, 2, 2)], # True - (2, 2) matches and 0 in third position
... [(1, 2, 0), (1, 0, 2)], # False
... ], columns=["Test1", "Test2"])
>>> df.apply(lambda x: compare_tuples(*x), axis=1)
0 False
1 False
2 True
3 True
4 False
dtype: bool
使用此功能,您可以比较两个元组并检查它们是否满足您的要求:
def eval_tuples(tup1, tup2):
# Check if zeros occures once in each tuple
if tup1.count(0) != 1 or tup2.count(0) != 1:
return False
# Get non-zero values
rem1 = [x for x in tup1 if x != 0]
rem2 = [x for x in tup2 if x != 0]
# Check if they are equal in both tuples
if rem1[0] != rem1[1] or rem2[0] != rem2[1]:
return False
return True
以下是该函数的一些示例运行:
print(eval_tuples([1,1,0], [1,0,1]))
# True
print(eval_tuples([1,1,0], [2,0,1]))
# False
print(eval_tuples([2,2,0], [0,2,2]))
# True
print(eval_tuples([1,2,0], [1,0,2]))
# False