How can I drop all rows in a Pandas dataframe except for specific ones?

How can I drop all rows in a Pandas dataframe except for specific ones?

The process of dropping specific rows in a Pandas dataframe involves removing all rows from the dataset except for the ones that meet certain criteria. This can be achieved by using the “drop” function in Pandas and specifying the desired rows to keep. This allows for selective manipulation of the dataframe, allowing for a more efficient and organized analysis of the data. By dropping all irrelevant rows, the resulting dataframe will only contain the necessary information, making it easier to draw accurate conclusions and insights. This method is commonly used in data preprocessing and cleaning, as well as in filtering out specific data points for further analysis.

Pandas: 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.

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

Cite this article

stats writer (2024). How can I drop all rows in a Pandas dataframe except for specific ones?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-all-rows-in-a-pandas-dataframe-except-for-specific-ones/

stats writer. "How can I drop all rows in a Pandas dataframe except for specific ones?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-all-rows-in-a-pandas-dataframe-except-for-specific-ones/.

stats writer. "How can I drop all rows in a Pandas dataframe except for specific ones?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-all-rows-in-a-pandas-dataframe-except-for-specific-ones/.

stats writer (2024) 'How can I drop all rows in a Pandas dataframe except for specific ones?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-all-rows-in-a-pandas-dataframe-except-for-specific-ones/.

[1] stats writer, "How can I drop all rows in a Pandas dataframe except for specific ones?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I drop all rows in a Pandas dataframe except for specific ones?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top