Runawaygeek
Runawaygeek

Reputation: 145

usecols in Python not reading the headings in my CSV

I have a CSV with Headings in row 1, Number and Arrival Date, these are columns 0 and 8.

When I write:

cv2 = pd.read_csv(honeyfile, skiprows=[0], header=None, usecols=[0,8])

It works fine.

however, I wanted to use a string in case more columns are ever added to the report and shifted the count.

I cut and paste the headings in from the CSV to make sure Case and white space were correct.

This is my code:

cv2 = pd.read_csv(honeyfile, skiprows=[0], header=None, usecols=['Number','Arrival Date'])

This returns a ValueError saying there are no matching columns with these names? While the error points to this line, it should be noted that the next line of code renames them,

cv2.columns = ['Supply Number','Delivery Date']

This works fine when I use the numbers to target the columns. Any ideas?

Thanks, Ben

Upvotes: 0

Views: 225

Answers (1)

jezrael
jezrael

Reputation: 863741

I think you need:

cv2 = pd.read_csv(honeyfile, usecols=['Number','Arrival Date'])

because is necessary read header to columns names.

skiprows=[0], header=None remove columns names, so only working usecols=[0,8].

Upvotes: 2

Related Questions