Reputation: 639
I have a list and pandas dataframe data that looks like this:
user_id = [10, 15, 20, 25, 30, 32, 40, 45, 50]
user_id value
10 45
20 49
25 19'
30 58
32 48
I've try to find user_id list not in list. My desired result is
result = [15, 40, 45, 50]
What is the easiest way to get desired result? (Currently I've got the result by for~loop)
Thank you.
Upvotes: 2
Views: 6141
Reputation: 296
You can just use Series.isin() with negation (~
).
df[~df["user_id"].isin(set(user_id))]
Conversion to set is always preferable as you'll better running time.
Upvotes: 3
Reputation: 2785
You can just change the user_id column to a list and then use list comprehension to find the ones that are in your original list not in the other list.
user_id = [10, 15, 20, 25, 30, 32, 40, 45, 50]
df = pd.DataFrame({'user_id': [10, 20, 25, 30, 32], 'value': [45, 49, 19, 58, 48]}
df_user_id = df['user_id'].tolist()
result = [x for x in user_id if x not in df_user_id]
[15, 40, 45, 50]
Upvotes: 1
Reputation: 19947
Use a set operation:
list(set(user_id)-set(df.user_id))
Out[84]: [40, 50, 45, 15]
Upvotes: 8