I have a pandas DataFrame like this:
In [34]: people = pandas.DataFrame({'name' : ['John', 'John', 'Mike', 'Sarah', 'Julie'], 'age' : [28, 18, 18, 2, 69]})
people = people[['name', 'age']]
people
Out[34]:
name age
0 John 28
1 John 18
2 Mike 18
3 Sarah 2
4 Julie 69
I want to filter this DataFrame using the following tuples:
In [35]: filter = [('John', 28), ('Mike', 18)]
The output should look like this:
Out[35]:
name age
0 John 28
2 Mike 18
I've tried doing this:
In [34]: mask = k.isin({'name': ['John', 'Mike'], 'age': [28, 18]}).all(axis=1)
k = k[mask]
k
However it shows me both Johns because it filters each column independently (the ages of both Johns are present in the age array).
Out[34]:
name age
0 John 28
1 John 18
2 Mike 18
How do I filter rows based on multiple fields taken together?