RustyShackleford
RustyShackleford

Reputation: 3677

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.

Upvotes: 1

Views: 1161

Answers (1)

jezrael
jezrael

Reputation: 863176

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]

Upvotes: 4

Related Questions