How can I use Pandas to drop columns not in a list?

Pandas can be used to drop columns that are not in a specified list by using the .drop() method and specifying the columns to be dropped in the argument. The syntax for this would be dataframe.drop(columns=[list of columns to be dropped], inplace=True). The inplace=True argument is necessary to make the changes permanent.


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.

x