0

I have a pandas dataframe with column headers, which contain information. I want to loop through the column headers and use logical operations on each header to extract the columns with the relevant information that I have.

my df.columns command gives something like this:

['(param1:x)-(param2:y)-(param3:z1)',
'(param1:x)-(param2:y)-(param3:z2)',
'(param1:x)-(param2:y)-(param3:z3)']

I want to select only the columns, which contain (param3:z1) and (param3:z3).

Is this possible?

NYC Coder
  • 7,424
  • 2
  • 11
  • 24
mel el
  • 481
  • 4
  • 6

1 Answers1

1

You can use filter:

df = df.filter(regex='z1|z3')
NYC Coder
  • 7,424
  • 2
  • 11
  • 24
  • Hi NYC Coder, this has been successful on my sample data. Tomorrow I shall try it on my full dataset and see if it works. Many thanks. – mel el Aug 19 '20 at 21:31