Reputation: 5097
I have a data-frame df
where the head looks like:
id dept paid
97 accounts monthly
73 shop weekly
20 marketing yearly
70 analysis monthly
55 advisory monthly
42 marketing monthly
76 marketing weekly
I am interested in looking at a subset of employees from a subset of dept and of these a subset of paid.
So for example if I wanted to return a subset of the data-frame that was marketing
and advisory
and of these only those paid monthly
and weekly
I would return the following complete records:
id dept paid
55 advisory monthly
42 marketing monthly
76 marketing weekly
I have tried to use two lists called deptQuery and paidQuery which look like:
deptQuery = ['marketing','advisory']
paidQuery = ['monthly','weekly']
and apply it to:
queryResult = df[df['dept'].isin(deptQuery) and DeptTemplate['paid'].isin(paidQuery)]
I also tried
queryResult = df[df['dept'].isin(deptQuery)] and df[df['paid'].isin(paidQuery)]
but I can't get it to work. What other things could I try?
Upvotes: 1
Views: 101
Reputation: 862541
Use & for bitwise AND
and remove df[
:
queryResult = df[df['dept'].isin(deptQuery) & df['paid'].isin(paidQuery)]
print (queryResult)
id dept paid
4 55 advisory monthly
5 42 marketing monthly
6 76 marketing weekly
Another solution with query
:
queryResult = df.query('dept in @deptQuery & paid in @paidQuery')
Upvotes: 4