Erich Purpur
Erich Purpur

Reputation: 1519

Pandas - Drop row from list of values

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

Upvotes: 2

Views: 2133

Answers (1)

Celius Stingher
Celius Stingher

Reputation: 18377

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

Upvotes: 3

Related Questions