Vincent Liu
Vincent Liu

Reputation: 11

Filtering list elements on Pandas

I have a pandas dataframe, foo, that looks like the following:

   a         b
0  1    [1, 2]
1  2    [1, 2]
2  3  [10, 11]

and I am trying to retrieve all the rows that contain the element [1, 2] in column b. Is there a good way to do this?

Upvotes: 0

Views: 44

Answers (1)

Alex
Alex

Reputation: 19104

The standard way to do this is with boolean indexing:

mask = df['b'].apply(lambda x: x == [1, 2])
df[mask]

returns

   a       b
0  1  [1, 2]
1  2  [1, 2]

If you are new to pandas, the new user tutorial is a good place to start and will cover questions like this.

Upvotes: 2

Related Questions