Demaunt - 9 months ago 40

Python Question

I am new to pandas and python.

I want to use dictionary to filter DataFrame

`import pandas as pd`

from pandas import DataFrame

df = DataFrame({'A': [1, 2, 3, 3, 3, 3], 'B': ['a', 'b', 'f', 'c', 'e', 'c'], 'D':[0,0,0,0,0,0]})

my_filter = {'A':[3], 'B':['c']}

When I call

`df[df.isin(my_filter)]`

I get

`A B D`

0 NaN NaN NaN

1 NaN NaN NaN

2 3.0 NaN NaN

3 3.0 c NaN

4 3.0 NaN NaN

5 3.0 c NaN

What I want is

`A B D`

3 3.0 c 0

5 3.0 c 0

I dont want to add "D" in the dictionary, I want to get rows that has proper values in A and B clumns

Answer

You can `sum`

of `True`

by columns and then compare with `2`

:

```
print (df.isin(my_filter).sum(1) == 2)
0 False
1 False
2 False
3 True
4 False
5 True
dtype: bool
print (df[df.isin(my_filter).sum(1) == 2])
A B D
3 3 c 0
5 3 c 0
```

Another solution with first filter only columns with condition `A`

and `B`

with `all`

for checking both `True`

by columns:

```
print (df[df[['A','B']].isin(my_filter).all(1)])
A B D
3 3 c 0
5 3 c 0
```

Thank you `MaxU`

for more flexible solution:

```
print (df[df.isin(my_filter).sum(1) == len(my_filter.keys())])
A B D
3 3 c 0
5 3 c 0
```