Pandas Indexing Using Multiple Isin Clauses
If I want to do is-in testing on multiple columns at once, I can do: >>> from pandas import DataFrame >>> df = DataFrame({'A': [1, 2, 3], 'B': [1, 4, 7], 'C' : [
Solution 1:
You could put both the isin
conditions in &
df[df['A'].isin([1, 3]) & df['B'].isin([4, 7, 12])]
AB C
23718
You could also use query
function like
c_a = [1, 3]
c_b = [4, 7, 12]
df.query('(B in @c_b) & (A in @c_a)')
A B C
2 3 7 18
Solution 2:
TBH, your current approach looks fine to me; I can't see a way with isin
or filter
to improve it, because I can't see how to get isin
to use only the columns in the dictionary or filter
to behave as an all
.
I don't like hardcoding column names, though, so I'd probably write this as
>>>keep = {'A': [1, 3], 'B': [4, 7, 12]}>>>df[df[list(keep)].isin(keep).all(axis=1)]
A B C
2 3 7 18
or with .loc
if I needed a handle.
Solution 3:
You could put both conditions in as a mask and use &
:
In[12]:
df[(df['A'].isin([1,3])) & (df['B'].isin([4,7,12]))]
Out[12]:
ABC23718
Here the conditions require parentheses ()
around them due to operator precedence
Slightly more readable is to use query
:
In[15]:
df.query('A in [1,3] and B in [4,7,12]')
Out[15]:
ABC23718
Post a Comment for "Pandas Indexing Using Multiple Isin Clauses"