Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore missing columns in 'usecol' parameter

Tags:

python

pandas

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.

like image 646
triphook Avatar asked Jun 10 '26 20:06

triphook


1 Answers

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
like image 66
ALollz Avatar answered Jun 13 '26 11:06

ALollz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!