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.:
codes = [6757,1234, 5674, 9990,1110,5678,4532,1123,3456,7865]
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:
data.loc[:, data.columns.str.contains(codes)]
but i get the error: TypeError: unhashable type: 'list'
. How can i go about this?
CodePudding user response:
Convert values to strings and join by |
for regex OR
:
data.loc[:, data.columns.str.contains('|'.join(map(str, codes)))]
Or use DataFrame.filter
:
data.filter(regex='|'.join(map(str, codes)))