Jack Koh
Jack Koh

Reputation: 73

Dropping values from the list

I got a multiple dataframe which looks like this:

A = [0,0,0,1,0,-1,2,-3,0,4,-4]
B = [0,0,0,1,-5,0,5,-6,0,4,-3,5,-6]

I wan to return a return where the first entry of the dataFrame is a -ve value where the last data is a +ve value.

Output

A = [-1,2,-3,4]
B = [-5,5,-6,4,-3,5]

How can I run a loop to look through all the dataframe? The length of the dataframe is different.

Upvotes: 1

Views: 71

Answers (1)

Nihal
Nihal

Reputation: 5334

code:

a = [0,0,0,1,0,-1,2,-3,0,4,-4]
b = [0,0,0,1,-5,0,5,-6,0,4,-3,5,-6]

def arr(x):
    x[:] = (value for value in x if value != 0)
    while x[0] > 0:

        x.pop(0)

    while x[-1] < 0:
        x.pop()
    # print(x)
    return x

print(arr(a))
print(arr(b))

output:

[-1, 2, -3, 4]
[-5, 5, -6, 4, -3, 5]

you can also use x.pop(0) instead of del x[0]

Upvotes: 3

Related Questions