Reputation: 19456
I can select a subset using Boolean indexing as such:
df.loc[df['my column'] == 1]
What I'd like to do is replace the == 1
with a list, [1, 2]
.
Something like:
df.loc[df['my column'] in [1,2]]
This is the equivalent of a long or statement.
How do I do it nicely with pandas?
(alternative, non-Boolean answers are good too).
Upvotes: 0
Views: 82
Reputation: 393963
Use isin
to test membership in a list of values:
df.loc[df['my column'].isin([1,2])]
Example:
In [18]:
df = pd.DataFrame({'a':np.random.randn(5), 'b':np.arange(5)})
df
Out[18]:
a b
0 -0.118930 0
1 -0.129362 1
2 0.822227 2
3 1.781672 3
4 -0.127322 4
In [19]:
df.loc[df['b'].isin([2,4])]
Out[19]:
a b
2 0.822227 2
4 -0.127322 4
Upvotes: 3