how to ignore the first column in a csv file when I import it into pandas?

You can use the pandas read_csv function and specify the parameter “usecols” to indicate which columns to use when importing the CSV file. The first column can be ignored by not including it in the list of columns specified for the “usecols” parameter.


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.

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

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

 

x