Extracting column containing one of two string possibilities

Viewed 23

I am looping through many txt files which have inconsistent data naming practices. I would like to extract a column which contains specific data, however it has a few different names depending on the file, and often a different location within the header column.

So far I have:

if "Var_version1" in df1.columns or 'Var_version2' in df1.columns: 
    df2 = df1[["Other_var1","Other_var2","Var_version1"]].copy()

The if or loop is correct, but the extraction is a hold over from before I realized the different naming conventions. How do I extract the entire column if the header contains a particular string or an alternate string within its name? (note: the header name may be xxxxVar_version1xxxx, not just Var_verison1) Thank you!

1 Answers

You can use a regex, or list of possibilities combined with filter:

sel = df1.filter(regex='Var_version[12]').columns.to_list()
df2 = df1[["Other_var1","Other_var2"]+sel]

or:

import re
possibilities = ['Var_version1', 'Var_version2']
sel = (df1.filter(regex='|'.join(re.escape(x) for x in possibilities))
          .columns.to_list())
df2 = df1[["Other_var1","Other_var2"]+sel]
Related