I have a Pandas dataframe with multiple columns and I would like to filter it to get a subset that matches certain values in different columns. I used the isin() method and passed a dictionary but I keep getting the TypeError with the message TypeError: only list-like or dict-like objects are allowed to be passed to DataFrame.isin(), you passed a 'str'. 
Following is an example of the problem:
x = pd.DataFrame({'surname':['wang','park','park'],'name':['tim','john','sam']})
display(x)
filt_dict = {'surname':'park'}
x[x.isin(filt_dict)]
I expected two rows where the surname matches park, but I get the TypeError instead.
What gives?
As you need to filter by dict values:
In [118]: x[x.surname.isin(filt_dict.values())]
Out[118]: 
  surname  name
1    park  john
2    park   sam
                        The values of the filtering dict must be lists:
x = pd.DataFrame({'surname': ['wang', 'park', 'park'], 'name': ['tim', 'john', 'sam']})
d = {'surname': ['park']}
print(x.isin(d))
Output
   surname   name
0    False  False
1     True  False
2     True  False
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With