failsafe100
failsafe100

Reputation: 133

usecols to be case insensitive in pandas

I am trying to cycle through large number of CSVs with same column names, however some of them have a different case.

I am using "usecols" in my code which throws an error when the case does not match mycols list.

df=pd.read_csv(fname,sep=",", encoding="ISO-8859-1",quotechar='"',error_bad_lines=False,dtype=object,usecols=mycols,index_cols=False)

How do I make sure that usecols ignores the case?

Thanks

Upvotes: 4

Views: 2389

Answers (1)

Igor Rivin
Igor Rivin

Reputation: 4864

From the pandas documentation (describing the callable version of usecols)

If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. An example of a valid callable argument would be lambda x: x.upper() in ['AAA', 'BBB', 'DDD']. Using this parameter results in much faster parsing time and lower memory usage.

Upvotes: 4

Related Questions