You can use the following methods to only keep certain columns in a pandas DataFrame:
Method 1: Specify Columns to Keep
#only keep columns 'col1' and 'col2' df[['col1', 'col2']]
Method 2: Specify Columns to Drop
#drop columns 'col3' and 'col4' df[df.columns[~df.columns.isin(['col3', 'col4'])]]
Method 3: Use the drop() Method
#drop columns 'col3' and 'col4' df.drop(['col3', 'col4'], axis=1)
Method 4: Use the filter() Method
#only keep columns that start with 'col' df.filter(like='col', axis=1)
The following examples show how to use each method with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'B'], 'points': [11, 7, 8, 10, 13, 13], 'assists': [5, 7, 7, 9, 12, 9], 'rebounds': [11, 8, 10, 6, 6, 5]}) #view DataFrame df team points assists rebounds 0 A 11 5 11 1 A 7 7 8 2 A 8 7 10 3 B 10 9 6 4 B 13 12 6 5 B 13 9 5
Method 1: Specify Columns to Keep
The following code shows how to define a new DataFrame that only keeps the “team” and “points” columns:
#create new DataFrame and only keep 'team' and 'points' columns
df2 = df[['team', 'points']]
#view new DataFrame
df2
team points
0 A 11
1 A 7
2 A 8
3 B 10
4 B 13
5 B 13
Notice that the resulting DataFrame only keeps the two columns that we specified.
Method 2: Specify Columns to Drop
The following code shows how to define a new DataFrame that drops the “assists” and “rebounds” columns from the original DataFrame:
#create new DataFrame and that drops 'assists' and 'rebounds'
df2 = df[df.columns[~df.columns.isin(['assists', 'rebounds'])]]
#view new DataFrame
df2
team points
0 A 11
1 A 7
2 A 8
3 B 10
4 B 13
5 B 13
Notice that the resulting DataFrame drops the “assists” and “rebounds” columns from the original DataFrame and keeps the remaining columns.
Method 3: Use the drop() Method
The following code shows how to define a new DataFrame that drops the “assists” and “rebounds” columns using the drop() method:
#create new DataFrame that drops 'assists' and 'rebounds' using drop() method
df2 = df.drop(['assists', 'rebounds'], axis=1)
#view new DataFrame
df2
team points
0 A 11
1 A 7
2 A 8
3 B 10
4 B 13
5 B 13
The drop() method offers a clear syntax for removing specified columns. When using drop(), you need to set axis=1 to indicate you’re dropping columns (not rows). This method is often preferred by many pandas users for its readability.
Method 4: Use the filter() Method
The following code shows how to use the filter() method to keep only columns with specific patterns:
#create new DataFrame that only keeps columns that contain the letter 't'
df2 = df.filter(like='t', axis=1)
#view new DataFrame
df2
team points assists
0 A 11 5
1 A 7 7
2 A 8 7
3 B 10 9
4 B 13 12
5 B 13 9
The filter() method allows you to select columns based on patterns in their names. This is particularly useful when working with datasets that have many columns following a naming convention. In this example, we kept only the columns containing the letter ‘t’ (team, points, and assists).
#create new DataFrame that only keeps columns that contain the letter 'e'
df3 = df.filter(like='e', axis=1)
#view new DataFrame
df3
team rebounds
0 A 11
1 A 8
2 A 10
3 B 6
4 B 6
5 B 5
In this second filter() example, we kept all columns containing the letter ‘e’ (team and rebounds).
Wrapping Up
Each method for keeping specific columns in a pandas DataFrame has its advantages:
- Use the direct indexing method (df[[‘col1’, ‘col2’]]) when you have a simple list of columns to keep
- Use the ~df.columns.isin([‘col1’, ‘col2’]) approach when you need to exclude specific columns
- Use the drop() method when you prefer a more readable syntax for excluding columns
- Use the filter() method when you need to select columns based on patterns in their names
Select the most appropriate method based on your specific needs and the structure of your dataset. Using these techniques will help you maintain cleaner, more focused DataFrames for your analysis.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Drop First Column in Pandas DataFrame
How to Drop Duplicate Columns in Pandas
How to Drop Columns by Index in Pandas