20.7 C
London
Monday, June 2, 2025
HomePandas in PythonInput/Output in PythonPandas: Drop Specific Column when Importing CSV File

Pandas: Drop Specific Column when Importing CSV File

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.

Additional Resources

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

Pandas: How to Skip Rows when Reading CSV File
Pandas: How to Append Data to Existing CSV File
Pandas: How to Use read_csv with usecols Argument

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories