Home > other >  How to remove both positive and negative numbers from a list in dataframe?
How to remove both positive and negative numbers from a list in dataframe?

Time:06-09

I have a dataframe which contains two list, how to drop both positive and negative number from the lists? Here is my data: https://github.com/mayuripandey/Data-Analysis/blob/main/similarity.csv

Input: [0, hi. hello, okay, -3]

Expected output: [hi, hello, okay]

CodePudding user response:

I would use python's built-in lstrip to remove the plus or minus at the start and check if it's numeric using the isnumeric method.

lst = ["0", "hi", "hello", "okay", "-3"]
is_num = lambda s : not s.lstrip(' -').isnumeric()
print(list(filter(is_num, lst))) # or [x for x in lst if is_num(x)]
# output : ['hi', 'hello', 'okay'] 

CodePudding user response:

One way to do this is with exception handling

def is_int(x):
    try:
        int(x)
    except ValueError:
        return False
    else:
        return True

input_ = [0, "hi", "hello", "okay", -3]
out = [i for i in input_ if not is_int(i)]  
  • Related