Pandas: How to Drop All Rows Except Specific Ones


You can use the following methods to drop all rows except specific ones from a pandas DataFrame:

Method 1: Drop All Rows Except Those with Specific Value in Column

#drop all rows except where team column is equal to 'Mavs'
df = df.query("team == 'Mavs'")

Method 2: Drop All Rows Except Those with One of Several Specific Values in Column

#drop all rows except where team is equal to 'Mavs' or 'Heat'
df = df.query("team == 'Mavs' | team == 'Heat'")

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Mavs', 'Heat', 'Heat', 'Cavs', 'Cavs'],
                   'points': [18, 22, 19, 14, 14, 11],
                   'assists': [5, 7, 7, 9, 12, 9]})

#view DataFrame
print(df)

   team  points  assists
0  Mavs      18        5
1  Mavs      22        7
2  Heat      19        7
3  Heat      14        9
4  Cavs      14       12
5  Cavs      11        9

Example 1: Drop All Rows Except Those with Specific Value in Column

We can use the following syntax to drop all rows except those with a value of ‘Mavs’ in the team column:

#drop all rows except where team column is equal to 'Mavs'
df = df.query("team == 'Mavs'")

#view updated DataFrame
print(df)

   team  points  assists
0  Mavs      18        5
1  Mavs      22        7

Notice that every row has been dropped except the rows that have a value of ‘Mavs’ in the team column.

Example 2: Drop All Rows Except Those with One of Several Specific Values in Column

We can use the following syntax to drop all rows except those with a value of ‘Mavs’ or ‘Heat’ in the team column:

#drop all rows except where team column is equal to 'Mavs'
df = df.query("team == 'Mavs' | team == 'Heat'")

#view updated DataFrame
print(df)

   team  points  assists
0  Mavs      18        5
1  Mavs      22        7
2  Heat      19        7
3  Heat      14        9

Notice that every row has been dropped except the rows that have a value of ‘Mavs’ or ‘Heat’ in the team column.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Drop First Row in Pandas DataFrame
How to Drop First Column in Pandas DataFrame
How to Drop Duplicate Columns in Pandas

Leave a Reply

Your email address will not be published.