30

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))
0

2 Answers 2

48

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

pd.read_csv(r"C:\test.csv",usecols=[2])
Sign up to request clarification or add additional context in comments.

1 Comment

Not to add much to this answer, but of course, you can select various columns, as I did here: df = pd.read_csv( '.\\file.csv', usecols=[0,1,2,3,5])
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.