Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using pandas.read_csv to read certain columns

Tags:

python

pandas

csv

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)) 
like image 253
Daniel Dahms Avatar asked Aug 02 '16 09:08

Daniel Dahms


People also ask

How do I read a specific column in pandas?

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.


2 Answers

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

pd.read_csv(r"C:\test.csv",usecols=[2]) 
like image 140
EdChum Avatar answered Oct 09 '22 12:10

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

like image 45
Kenan Avatar answered Oct 09 '22 13:10

Kenan