finding a list stored in dataframe cell

Viewed 85

I have a dataframe df like this:

    values
0    [0,1]
1    [1,2]
2    [2,3]
3    [3,4]
4    [4,5]

Something like this doesn't work:

df[df['values'] == [0,1]]

I get:

ValueError: 'Lengths must match to compare', (5,), (2,)

How can I get a row given the list stored in that row?

2 Answers

Use list comprehenion:

[i == [0,1] for i in df['values']]

Like this:

df[[i == [0,1] for i in df['values']]]

Output:

   values
0  [0, 1]
df.loc[(df['values'].apply(pd.Series) == [0,1]).all(axis=1)]
Related