3.1 C
London
Friday, December 20, 2024
HomePandas in PythonInput/Output in PythonPandas: Ignore First Column when Importing CSV File

Pandas: Ignore First 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 ignore the first column when importing a CSV file into a pandas DataFrame:

with open('basketball_data.csv') as x:
    ncols = len(x.readline().split(','))

df = pd.read_csv('basketball_data.csv', usecols=range(1,ncols))

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

Using this code, we first find the number of columns in the CSV file and assign it to a variable called ncols.

Then we use the usecols argument to specify that we only want to import the columns in the range from 1 (i.e. the second column) to the last column of the CSV file.

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

Example: Ignore First 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 a pandas DataFrame and ignore the first column:

import pandas as pd

#calculate number of columns in CSV file
with open('basketball_data.csv') as x:
    ncols = len(x.readline().split(','))

#import all columns except first column into DataFrame
df = pd.read_csv('basketball_data.csv', usecols=range(1,ncols))

#view resulting DataFrame
print(df)

   points  rebounds
0      22        10
1      14         9
2      29         6
3      30         2

Notice that the first column called team was dropped when we imported the CSV file into pandas.

Note that if you already know the total number of columns in the CSV file ahead of time, you can directly supply that value to the usecols argument.

For example, suppose we already knew that there were three columns in the CVS file.

We could use the following syntax to import the CSV file into a pandas DataFrame and ignore the first column:

import pandas as pd

#import all columns except first column into DataFrame
df = pd.read_csv('basketball_data.csv', usecols=range(1,3))

#view resulting DataFrame
print(df)

   points  rebounds
0      22        10
1      14         9
2      29         6
3      30         2

Notice that the first column called team was dropped when we imported the CSV file into pandas.

Since we already knew there were three columns in the CSV file, we simply used range(1,3) in the usecols argumnent.

Note: You can find the complete documentation for the pandas read_csv() function here.

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