'Check if terms are in columns and remove

Originally I wanted to filter only for specific terms, however I've found python will match the pattern regardless of specificity eg:

possibilities = ['temp', 'degc']
temp = (df.filter(regex='|'.join(re.escape(x) for x in temp_possibilities))
                  .columns.to_list())

Output does find the correct columns, but unfortunately it also returns columns like temp_uncalibrated, which I do not want.

So to solve this, so far I define and remove unwanted columns first, before filtering ie:

    if 'temp_uncalibrated' in df.columns:
        df = df.drop('temp_uncalibrated',axis = 1)
    else:
        pass

However, I have found more and more of these unwanted columns, and now the code looks messy and hard to read with all the terms. Is there a way to do this more succinctly? I tries putting the terms in a list and do it that way, but it does not work, ie:

    if list in df.columns:
        df = df.drop(list,axis = 1)
    else:
        pass

I thought maybe a def function might be a better way to do it, but not really sure where to start.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source