Search code examples
pythonpython-3.xlistpandaslist-comparison

How to compare list against column of dataframe, and remove elements from list if elements match between dataframe column and list?


I have a df that looks like this:

number
1
2
3
4

A list that looks like this:

lst = [1,2,3,4,5]

How do I compare the list and the column in dataframe, while removing the elements in the list that match in the dataframe?

New list would be:

lst = [5]

To add the column in the datafrrame has many numbers that repeat so only need to remove the number once if it is in both list and column.


Solution

  • Use numpy.setdiff1d or substract sets:

    df = pd.DataFrame([1,2,3,4],columns=['number']) 
    print (df)
    
    lst = [1,2,3,4,5]
    
    L = np.setdiff1d(lst, df['number'])
    print (L)
    [5]
    

    Or:

    L = list(set(lst) - set(df['number']))
    print (L)
    [5]