Baron Yugovich
Baron Yugovich

Reputation: 4307

Pandas DataFrame - select rows that match one of many conditions

I want to select part of the dataframe, where the value in one particular column is one of multiple values.

What I do now is

idx = my_df['Column'].apply(lambda z: z in selected_items)
new_df = my_df[idx]

Is there a better way to achieve this?

Upvotes: 1

Views: 753

Answers (2)

EdChum
EdChum

Reputation: 393893

Use isin:

new_df = my_df[idx].isin(selected_items)

example:

In [579]:

df = pd.DataFrame({'a': [0, 1, 2, 3, 4, 5, 6, 7]})
​
df
Out[579]:
   a
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
In [581]:

df[df['a'].isin([3,5,6])]
Out[581]:
   a
3  3
5  5
6  6

Another method is query:

In [582]:

df.query('a in [3,5,6]')
Out[582]:
   a
3  3
5  5
6  6

Upvotes: 1

Zero
Zero

Reputation: 76917

This should work?

new_df = my_df[my_df['Column'].isin(selected_items)]

Upvotes: 2

Related Questions