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.
Use a callable checking if the column is in the subset
subset = ['a', 'c', 'd']
df = pd.read_csv('sample.csv', usecols=lambda x: x in subset)
a c
0 1 3
1 4 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With