Find the list values not in pandas dataframe data

前端 未结 3 2018
刺人心
刺人心 2021-01-26 09:08

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             


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 10:02

    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]
    

提交回复
热议问题