0

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

BPD
  • 3
  • 1
  • Filter certain columns, like column with certain 'string' in column names? – Abhishek Jul 06 '22 at 16:10
  • Welcome to stack overflow! In order to make it easier for people to answer your question, it helps to include a [mcve] with sample input and expected output, as well as code for what you've already tried and what went wrong with your attempts. Also worth checking out [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Jul 06 '22 at 16:16

1 Answers1

0

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)
SomeDude
  • 13,876
  • 5
  • 21
  • 44