How can I drop a specific column when importing a CSV file into Pandas?

When importing a CSV file into Pandas, you can specify the columns you would like to include in the dataframe by using the “usecols” parameter. By providing this parameter with a list of columns, Pandas will only include the columns specified and drop all other columns. This is useful when you want to quickly drop a specific column when importing a CSV file into Pandas.


You can use the following basic syntax to drop a specific column when importing a CSV file into a pandas DataFrame:

df = pd.read_csv('basketball_data.csv', usecols=lambda x: x != 'rebounds')

This particular example will read each column from a CSV file called basketball_data.csv into a pandas DataFrame except for the column called rebounds.

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

Example: Drop Specific Column when Importing CSV File in Pandas

Suppose we have the following CSV file called basketball_data.csv:

We can use the following syntax to import the CSV file into pandas and drop the column called rebounds when importing:

import pandas as pd

#import all columns except 'rebounds' into DataFrame
df = pd.read_csv('basketball_data.csv', usecols=lambda x: x != 'rebounds')

#view resulting DataFrame
print(df)

  team  points
0    A      22
1    B      14
2    C      29
3    D      30

Notice that the rebounds column was dropped when we imported the CSV file into pandas.

If you would like to drop multiple columns when importing, you can use the not in operator as follows:

import pandas as pd

#import all columns except 'team' and 'rebounds' into DataFrame
df=pd.read_csv('basketball_data.csv', usecols=lambda x: x not in ['team', 'rebounds'])

#view resulting DataFrame
print(df)

   points
0      22
1      14
2      29
3      30

Notice that the team and rebounds columns were both dropped when we imported the CSV file into pandas.

Note that you can include as many column names as you’d like in the list following the not in operator to drop as many columns as you’d like when importing a CSV file.

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

x