2

I am new to the python and pandas . Here I have the following dataframe which has two lists .

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

In this datframe I am trying while compairing the two lists .There are some conditions which only should return true. So,

Here if either side has 2 0's and 1 positive value and other side has the same positive values then it should return True otherwise False.

So In this case

[1,0,0]          [1,1,1]    
[2,2,2]          [0,0,2]

Here for both of them it will return true.

Now, what I have tried is like this

def check_two_zeros_onEither_side(tup1,tup2):
    count_on_previous = tup1.count(0)
    count_on_next = tup1.count(0)
    rem1 = [x for x in tup1 if x != 0]
    rem2 = [x for x in tup2 if x != 0]
    if count_on_previous == 2:
        if all([rem1[0] == rem2[0], rem1[0] == rem2[1]]):

But Here I am not able handle some exception cases like, index out of range like that.. Can any please help me with this ? Thanks. And also how do I achieve this ?

def check_two_zeros_onEither_side(tup1,tup2,ins):
    count_on_previous = tup1.count(0)
    count_on_next = tup2.count(0)
    print(count_on_previous,count_on_next)
    rem1 = [x for x in tup1 if x != 0]
    rem2 = [x for x in tup2 if x != 0]
    print(rem1,rem2,len(tup1),len(tup2))
    if count_on_previous == 2 and len(rem1) == 1 and len(rem2) == 3:
        if all( [rem1[0] == rem2[0], rem1[0] == rem2[1], rem1[0] == rem2[2]]):
            print("GOin insde one",ins)
            return True
    elif count_on_next == 2 and len(rem2) == 1 and len(rem1) == 3:
        if all([rem2[0] == rem1[0], rem2[0] == rem1[1], rem2[0] == rem1[2]]):
            print("GOin insde two",ins)
            return True
        else:
            return False
    else:
        return False

This is what I tried.. It is working but is there any another way to do this ?

ganesh kaspate
  • 1
  • 9
  • 41
  • 88

1 Answers1

0

What about the following

def compare(a, b):
    # Ensure there are two zeros on either side
    if a.count(0) == 2 or b.count(0) == 2:
        # Compute the intersection and ensure it's not zero
        return len(set(a).intersection(b)) is not 0
    return False

Here are the tests for it using Pytest.

import pytest

class TestDataframes:

    def test_frame_a_contains_two_zeros(self):
        a = [0, 0 ,1]
        b = [1, 1, 1]

        assert compare(a, b) is True


    def test_frame_b_contains_two_zeros(self):
            a = [2, 2, 2]
            b = [0, 0, 2]

            assert compare(a, b) is True

    def test_both_frames_contains_two_zeros(self):
            a = [0, 0, 2]
            b = [0, 0, 2]

            assert compare(a, b) is True

    def test_neither_contain_two_zeros(self):
            a = [0, 1, 2]
            b = [0, 1, 2]

            assert compare(a, b) is False

    def test_positive_number_doesnt_match(self):
            a = [2, 2, 2]
            b = [0, 0, 1]

            assert compare(a, b) is False

The compare function will return the following.

[0, 0 ,1] [1, 1, 1] #=> True
[2, 2, 2] [0, 0, 2] #=> True
[0, 0, 2] [0, 0, 2] #=> True
[0, 1, 2] [0, 1, 2] #=> False
[0, 2, 2] [0, 0, 1] #=> False
Stu
  • 421
  • 3
  • 7