Search code examples
pythondataframedrop

drop dataframe rows with a for loop


Suppose I have a list with wrong users

wrong_users = [1,5,8,9,10]

I want to use this list to drop those users from the data drame df_users containing a column with all the users

I tried using a for loop:

for x in wrong_users:
    if x == df_users['user']:
        df_users.drop([x])

but I get the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()


Solution

  • You don't need a loop for that, try this.

    wrong_users =  [1,5,8,9,10]
    
    df_users = df_users[~df_users['users'].isin(wrong_users)].reindex()