Pandas - Drop row from list of values

Erich Purpur :

I have a simple dataframe:

df = pd.DataFrame({'ID': [100, 101, 134, 139, 192],
               'Name': ['Tom', 'Dave', 'Steve', 'Bob', 'Jim']})

and a list of values:

id_list = [100, 139]

I want to drop the rows from my dataframe if the 'ID' column == one of the values in my id_list.

The desired output is...

    ID   Name
1  101   Dave
2  134  Steve
4  192    Jim
Celius Stingher :

You can use .isin() for the ID series preceded with ~. Essentialy this works like "Not in":

output_df = df[~df['ID'].isin(id_list)]

Output:

    ID  Name
1   101 Dave
2   134 Steve
4   192 Jim

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237609&siteId=1