Reputation: 4723
I have the following df:
These are the counts of prescribed drug before and after a patient's index date.
PREINDEX_N_DRUG_A_TAG POSTINDEX_N_DRUG_A_TAG PREINDEX_N_DRUG_B_TAG POSTINDEX_N_DRUG_B_TAG PREINDEX_N_DRUG_C_TAG POSTINDEX_N_DRUG_C_TAG
0 8 0 8 0 8
0 2 4 0 8 4
2 0 0 4 3 5
I like to filter patients by various combination and criteria.
I made a rigid function as follows:
def filter_data(df):
df = df[
(df['PREINDEX_N_DRUG_A_TAG']>8) &
(df['POSTINDEX_N_DRUG_A_TAG']==2) &
(df['PREINDEX_N_DRUG_B_TAG']==0) &
(df['POSTINDEX_N_DRUG_B_TAG']>=2)
]
However, I want to make the function in such a way that is more flexible, so that the users can specify how ever many of criteria and the form of the criteria.
For example, I would like a function to use a command_dict
information such as
command_dict = {
'Command1': ['PREINDEX_N_DRUG_A_TAG', '>', 8, '&'],
'Command2': ['POSTINDEX_N_DRUG_A_TAG', '==', 2, '&'],
'Command3': ['PREINDEX_N_DRUG_B_TAG', '==', 0, '&'],
'Command4': ['POSTINDEX_N_DRUG_B_TAG', '>=', 2, '&'],
'Command5': ['PREINDEX_N_DRUG_A_TAG', '>', 8, '&'],
'Command6': ['PREINDEX_N_DRUG_A_TAG', '>', 8, '&'],
}
And be able to perform the function as in the rigid function.
Upvotes: 1
Views: 126
Reputation: 323366
This is perfect use case for query
df.query((''.join([''.join(map(str,x)) for x in command_dict.values()])).strip('&'))
Upvotes: 2