Python Pandas Select columns contains different values

Viewed 45

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

1 Answers

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.

Related