-1

i have a data frame object as below

    Issue Type       Status  Reporter  Issue id
0   Eng Activity       Closed  snalanag     98908
1   Eng Activity  In Progress  snalanag     98452
2   Eng Activity       Closed  snalanag     98425
3     Dev-Defect       Closed  snalanag     97244
4     Dev-Defect       Closed  snalanag     96716
5   Eng Activity       Closed  snalanag     96698
6     Dev-Defect       Closed  snalanag     96696

Now i want to filter data based on multiple conditions.

Condition will be in dictionary. Example:{'Issue Type': 'Dev-Defect', 'Status': 'Closed', 'Reporter': 'snalanag'}

Based on the condition, i have to filter the data frame by forming the query dynamically. Please note that i need to filter the data by applying AND condition .

That means i have to apply the filter for the given data frame with "Issue Type = Dev-Defect","Status = Closed","Reporter = snalanag" at once.

Something like this but it should be generated dynamically based on the condition dictionary.

print (df[(df["Issue key"] == 'BUG-22212') & (df["Issue id"] == 97244) & (df["Status"] == 'Closed')])

2 Answers2

1

You can use .loc with numpy.logical_and in it to accommodate multiple logical expressions. Since numpy.logical_and can have only 2 expressions, reduce function can be used to make it work with multiple expressions. An explanation: Numpy logical_or for more than two arguments.

For your case we can use something like:

df.loc[np.logical_and.reduce((df["Issue Type"]=="Dev-Defect", df["Status"]=="Closed", df["Reporter"]=="snalanag"))]

Hope this helps!

EDIT

Since you are using a dictionary for conditions, following may help:

conditions = {'Issue Type': 'Dev-Defect', 'Status': 'Closed', 'Reporter': 'snalanag'}
df.loc[np.logical_and.reduce(list(map(lambda x: df[x]==conditions[x], conditions.keys())))]
  • Hi Dhanush, your logic is working. if i write your condition manually::: print (df.loc[np.logical_and.reduce((df["Issue Type"]=="Dev-Defect", df["Status"]=="Closed", df["Reporter"]=="snalanag"))]). But i want the condition to be generated dynamically based on the condition dictionary length. I can't hardcode the values of condition inside syntax – Siva Sai Kumar Nalanagula Aug 01 '18 at 03:20
  • @SivaSaiKumarNalanagula I have edited the answer to use a dictionary of conditions instead of hardcoded values. See whether it helps. – Dhanush1215 Aug 01 '18 at 05:07
  • Thank you so much Dhanush. It helped and working fine. Once again thanks for helping in time. – Siva Sai Kumar Nalanagula Aug 01 '18 at 07:41
0

You might be able to get away with something like this:

cond_dict = {'Issue Type': 'Dev-Defect', 'Status': 'Closed', 'Reporter': 'snalanag'}

final_mask = np.ones(len(orig_df)).astype(bool)

for this_cond in cond_dict:
    cond_dict = (final_mask ) & (orig_df[this_cond] == cond_dict[this_cond])

filtered_df = orig_df[final_mask]
Felipe D.
  • 1,157
  • 9
  • 19
  • Thank you so much for the code. But it is getting failed due to invalid comparison of dtype object. Getting exception "cannot compare a dtyped [bool] array with a scalar of type [bool]" – Siva Sai Kumar Nalanagula Jul 31 '18 at 17:41
  • There, I've made a small change. See if that helps! And if so, please consider marking the question as answered =) – Felipe D. Jul 31 '18 at 18:09
  • This didn't help. why because not all the columns are of same dtype. Some are object,some are bool, some are int64. But i wanted to get the filtered data after applying condition. My condition should be something like this:: print (df[(df["Issue Type"] == 'Dev-Defect') & (df["Issue id"] == 97244)]). Same condition i want to generate dynamically based on the condition dicctionary – Siva Sai Kumar Nalanagula Aug 01 '18 at 02:54