using pandas.read_csv to read certain columns

Question:

I have a .csv file with three columns and many rows. I am trying to use pandas to read only the third column.

right now I have:

import pandas as pd

pd.read_csv(r"C:test.csv",usecols=(3))
Asked By: Daniel Dahms

||

Answers:

column indexing is zero based, pass 2 to read the third column:

pd.read_csv(r"C:test.csv",usecols=[2])
Answered By: EdChum

Adding on to @EdChum answer, you can also simply use range

pd.read_csv(r"C:test.csv",usecols=range(5))

to read the first 5 columns. If you columns aren’t numeric you can always use header=None to have pandas ignore the columns

Answered By: Kenan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.