I have a large dataframe in which I'm trying to filter certain columns that contain a certain phrase, IE ('bps'). I'm looking to filter out those columns from the original dataset and then look to perform a calculation (divide by 10000), and then replace it back to the original dataset.
I had originally been able to filter the columns by the below, but not able to append it to the original dataset. Any suggestions on other ways of completing this? Was thinking of doing a function but was thinking the most clean way to do it.
raw = raw.filter(like = 'BPS', axis = 1) / 10000
CodePudding user response:
You can do:
bps_cols = df.columns[df.columns.str.contains('bps')]
non_bps_cols = df.columns[~df.columns.str.contains('bps')]
df = pd.concat([df[non_bps_cols], df[bps_cols].div(1000)], axis=1)
or
df = pd.concat([df.filter(regex='bps'), df.filter(regex='^((?!bps).)*$').div(1000)], axis=1)