3

I have a dataframe and want to only show columns with headers containing a particular string(s).

df.loc[:,df.columns.str.contains(['BB','TP'])]

So this would only show columns that contain BB or TP. This doesn't work for 2 conditions however.

3
  • 1
    Can you add some sample data and expected output? Commented Dec 12, 2018 at 12:03
  • Good idea, that works Commented Dec 12, 2018 at 12:13
  • df.loc[:,df.columns.str.contains('|'.join('BB','TP')] Commented Dec 12, 2018 at 12:14

1 Answer 1

3

Use:

L = ['BB','TP']
df.loc[:, df.columns.str.contains('|'.join(L)] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.