I have a dataframe containing counts of two things, which I've put in columns numA and numB. I want to find the rows where numA < x and numB < y, which can be done like so:
filtered_df = df[(df.numA < x) & (df.numB < y)]
This works when both numA and numB are present. However neither column is guaranteed to appear in the dataframe. If only one column exists, I would still like to filter the rows based on it. This could be easily coded with something along the lines of
if "numA" in df.columns:
filtered_df = df[df.numA < x]
if "numB" in df.columns:
filtered_df = filtered_df[filtered_df.numB < y]
But this seems very inefficient, especially since in reality I have 9 columns like this, and each of these requires the same check. Is there a way to achieve the same thing but with code that is more readable, easier to maintain and less tedious to write out?
numAandnumBalong the way depending on certain conditions. So ifnumAisn't present thendf.numA.isnull()returns an error:'DataFrame' object has no attribute 'numA'.