Suppose I have a dataframe:
df = pd.DataFrame(np.random.randint(0, 100, size=(100, 7)), columns=list('ABCDEFG'))
that I need to pull a subset of df
based on a series of conditions on selected columns but the columns are to be tuned. With said, the conditions are 'dynamic' that give various subsets, such as (to make the scenario easy, let's use all &
):
dfs = df[(df.A > 10) & (df.C > 5) & (df.E < 30)]
dfs = df[(df.C > 5) & (df.D < 0)]
dfs = df[(df.B > 20) & (df.E < 30) & (df.F > 7) & (df.G < 15)]
...
Question is, is it possible to write a function that takes the columns and their compared values as some kind of **kwargs
that returns different subset with different column list? Many thanks!
Please note sub-setting is not the main issue here. It's how we pass different combinations of different columns and their corresponding compared values as arguments.