Skip to content
Advertisement

How can i filter on column names part of which contain a value in a given list?

Not sure if this has been asked else where but i couldn’t find the relevant question if there was. I have a list of values e.g.:

JavaScript

i then have colnames such as:

col1_6757, col_1234, col1_5432, col1_1110, amount_1110, etc

I would like to filter on the pandas dataframe such that i only retrieve those colnames that contain the values in code list..

i’ve tried:

JavaScript

but i get the error: TypeError: unhashable type: 'list'. How can i go about this?

Advertisement

Answer

Convert values to strings and join by | for regex OR:

JavaScript

Or use DataFrame.filter:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement