Python Pandas – Delete rows based on a conditional expression

In this post we will be creating a Python script that will delete rows within a data frame based on a conditional expression. This means if a column meets our expression, the entire row will be removed.

See below a sample of our data frame for this example.

In this example we will be removing all rows that contain a ‘Rank’ less than 5, see the below for how this is done.

df = df.drop(df[df.Rank < 5].index)

The above would return the following as output.

   Rank                       Name Platform
4     5   Pokemon Red/Pokemon Blue       GB
5     6                     Tetris       GB
6     7      New Super Mario Bros.       DS
7     8                   Wii Play      Wii
8     9  New Super Mario Bros. Wii      Wii
9    10                  Duck Hunt      NES

Leave a Reply