darkyoda182
darkyoda182

Reputation: 15

python pandas filtering involving lists

I am currently using Pandas in python 2.7. My dataframe looks similar to this:

>>> df
        0
1  [1, 2]
2  [2, 3]
3  [4, 5]

Is it possible to filter rows by values in column 1? For example, if my filter value is 2, the filter should return a dataframe containing the first two rows.

I have tried a couple of ways already. The best thing I can think of is to do a list comprehension that returns the index of rows in which the value exist. Then, I could filter the dataframe with the list of indices. But, this would be very slow if I want to filter multiple times with different values. Ideally, I would like something that uses the build in Pandas functions in order to speed up the process.

Upvotes: 0

Views: 3013

Answers (2)

Alexander
Alexander

Reputation: 109526

You can also use boolean indexing with a list comprehension:

>>> df[[2 in row for row in df['0']]]
        0
0  [1, 2]
1  [2, 3]

Upvotes: 1

jezrael
jezrael

Reputation: 862511

You can use boolean indexing:

import pandas as pd

df = pd.DataFrame({'0':[[1, 2],[2, 3], [4, 5]]})
print (df)
        0
0  [1, 2]
1  [2, 3]
2  [4, 5]

print (df['0'].apply(lambda x: 2 in x))
0     True
1     True
2    False
Name: 0, dtype: bool

print (df[df['0'].apply(lambda x: 2 in x)])
        0
0  [1, 2]
1  [2, 3]

Upvotes: 2

Related Questions