I'm reading a table from csv and only want a subset of the columns. The list I'm using to subset contains field names that may not exist in the table I'm reading. For example:
# contents of sample.csv:
#a,b,c
#1,2,3
#4,5,6
subset = ['a', 'c', 'd']
I'd like to return the following, using pandas.read_csv and the subset, but this raises an error:
pd.read_csv(sample.csv, usecols=subset)
a c
1 3
4 6
ValueError: Usecols do not match columns, columns expected but not found: ['d']
I think I might be able to do with a callable value for usecols, but am not sure how to implement.