You’ll be able to usefulness refer to plain syntax to forget about the primary column when uploading a CSV record right into a pandas DataFrame:
with evident('basketball_data.csv') as x: ncols = len(x.readline().fracture(',')) df = pd.read_csv('basketball_data.csv', usecols=field(1,ncols))
This actual instance will learn every column from a CSV record known as basketball_data.csv right into a pandas DataFrame excluding for the primary column.
The use of this code, we first in finding the collection of columns within the CSV record and assign it to a variable known as ncols.
After we usefulness the usecols argument to specify that we handiest need to import the columns within the field from 1 (i.e. the second one column) to the terminating column of the CSV record.
Refer to instance presentations tips on how to usefulness this syntax in observe.
Instance: Forget about First Column when Uploading CSV Report in Pandas
Assume we have now refer to CSV record known as basketball_data.csv:
We will be able to usefulness refer to syntax to import the CSV record right into a pandas DataFrame and forget about the primary column:
import pandas as pd #calculate collection of columns in CSV record with evident('basketball_data.csv') as x: ncols = len(x.readline().fracture(',')) #import all columns excluding first column into DataFrame df = pd.read_csv('basketball_data.csv', usecols=field(1,ncols)) #view ensuing DataFrame print(df) issues rebounds 0 22 10 1 14 9 2 29 6 3 30 2
Understand that the primary column known as staff used to be dropped after we imported the CSV record into pandas.
Word that if you know the entire collection of columns within the CSV record forward of presen, you’ll without delay provide that worth to the usecols argument.
As an example, assume we already knew that there have been 3 columns within the CVS record.
Shall we usefulness refer to syntax to import the CSV record right into a pandas DataFrame and forget about the primary column:
import pandas as pd #import all columns excluding first column into DataFrame df = pd.read_csv('basketball_data.csv', usecols=field(1,3)) #view ensuing DataFrame print(df) issues rebounds 0 22 10 1 14 9 2 29 6 3 30 2
Understand that the primary column known as staff used to be dropped after we imported the CSV record into pandas.
Since we already knew there have been 3 columns within the CSV record, we merely impaired field(1,3) within the usecols argumnent.
Word: You’ll be able to in finding your complete documentation for the pandas read_csv() serve as right here.
Backup Sources
Refer to tutorials provide an explanation for tips on how to carry out alternative ordinary duties in Python:
Pandas: Tips on how to Skip Rows when Studying CSV Report
Pandas: Tips on how to Append Information to Current CSV Report
Pandas: Tips on how to Significance read_csv with usecols Argument