Pandas: handle missing column

Viewed 3341

I'm using the following code to read a CSV file in chunks using pandas read_csv

headers = ["1","2","3","4","5"]
fields = ["1", "5"]

for chunk in pandas.read_csv(fileName, names=headers, header=0, usecols=fields, chunksize=chunkSize):

Sometimes my CSV won't have column "5" and I want to be able to handle this case and specify some default values. Is there a way to read just the headers of my CSV file without reading the whole file so I can handle this manually? Or may be any other clever way to default the value for the missing column?

1 Answers
Related