I am trying to perform isin() using df.filter but I need to filter columns dynamically based on user arguments passed. I am appending custom columnns to list and join later which converts it into string, since isin() is also getting converted into string it throws error. how do I keep isin as expression and not as str.
tmp=[]
count = 0
if bl is not None:
tmp.append(f"df.b.isin({args.bl})")
count += 1
if cl is not None:
tmp.append(f"df.cl.isin({args.cl})")
count += 1
if cl_g is not None:
tmp.append(f"df.cl_g.isin({args.cl_g})")
count += 1
if pl is not None:
tmp.append(f"df.pl.isin({args.pl})")
count += 1
if pl_b is not None:
tmp.append(f"df.pl_b.isin({args.pl_b})")
count += 1
if count >1:
return df.filter(" and ".join(tmp))
elif count > 0:
return df.filter("".join(tmp))