-5

I would like to select from DF columns contains only PF_20 and PF_70 values Original Table: enter image description here

Expected Result:

enter image description here

Thank you in advance

Felix
  • 1,539
  • 8
  • 20
  • 35
  • 3
    Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Sep 21 '22 at 18:44
  • 2
    Apart from the proper guide from the first, you may consider markdown-friendly output format that you may then copy into your question to provide your DataFrame in a nice-looking, yet not-so-compact table form. Just print your DataFrame out like this: [`print(df.to_markdown())`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_markdown.html) – Nikita Shabankin Sep 21 '22 at 19:05

1 Answers1

0

This might not be the fastest way to do it, but try this:

values = ["PF_20", "PF_70"]
columns = []
for col in df.columns:
    for value in values:
        if value in df[col].values:
            columns.append(col)
columns = set(columns) #to get rid of duplicates.
new_df = df[columns]

i am pretty sure there is a better way to do this and faster too, i hope somebody points it out.

Echo
  • 293
  • 2
  • 10