How do I drop columns in a Pandas dataframe that are not in a specified list?

How do I drop columns in a Pandas dataframe that are not in a specified list?

The process of dropping columns in a Pandas dataframe that are not included in a specified list involves using the “drop” function with the “columns” parameter and specifying the list of columns to be kept. This will remove all columns that are not included in the specified list, effectively reducing the size of the dataframe. This method is useful for data cleaning and organizing tasks, as it allows for the removal of unnecessary columns and focuses on the columns that are relevant for analysis.

Pandas: Drop Columns Not in List


You can use the following basic syntax to drop columns from a pandas DataFrame that are not in a specific list:

#define columns to keep
keep_cols = ['col1', 'col2', 'col3']

#create new dataframe by dropping columns not in list
new_df = df[df.columns.intersection(keep_cols)]

This particular example will drop any columns from the DataFrame that are not equal to col1, col2, or col3.

The following example shows how to use this syntax in practice.

Example: Drop Columns Not in List in Pandas

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12],
                   'steals': [4, 4, 10, 12, 8, 5, 5, 2]})

#view DataFrame
print(df)

  team  points  assists  rebounds  steals
0    A      18        5        11       4
1    B      22        7         8       4
2    C      19        7        10      10
3    D      14        9         6      12
4    E      14       12         6       8
5    F      11        9         5       5
6    G      20        9         9       5
7    H      28        4        12       2

Now suppose that we would like to create a new DataFrame that drops all columns that are not in the following list of columns: team, points, and steals.

We can use the following syntax to do so:

#define columns to keep
keep_cols = ['team', 'points', 'steals']

#create new dataframe by dropping columns not in list
new_df = df[df.columns.intersection(keep_cols)]

#view new dataframe
print(new_df)

  team  points  steals
0    A      18       4
1    B      22       4
2    C      19      10
3    D      14      12
4    E      14       8
5    F      11       5
6    G      20       5
7    H      28       2

Notice that each of the columns from the original DataFrame that are not in the keep_cols list have been dropped from the new DataFrame.

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

Cite this article

stats writer (2024). How do I drop columns in a Pandas dataframe that are not in a specified list?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-drop-columns-in-a-pandas-dataframe-that-are-not-in-a-specified-list/

stats writer. "How do I drop columns in a Pandas dataframe that are not in a specified list?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-drop-columns-in-a-pandas-dataframe-that-are-not-in-a-specified-list/.

stats writer. "How do I drop columns in a Pandas dataframe that are not in a specified list?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-drop-columns-in-a-pandas-dataframe-that-are-not-in-a-specified-list/.

stats writer (2024) 'How do I drop columns in a Pandas dataframe that are not in a specified list?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-drop-columns-in-a-pandas-dataframe-that-are-not-in-a-specified-list/.

[1] stats writer, "How do I drop columns in a Pandas dataframe that are not in a specified list?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I drop columns in a Pandas dataframe that are not in a specified list?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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