Compare lists in pandas DataFrame

I have a DataFrame in pandas, with one of the column types being a list on int, for example:

df = pandas.DataFrame([[1,2,3,[4,5]],[6,7,8,[9,10]]], columns=['a','b','c','d'])
>>> df
   a  b  c        d
0  1  2  3   [4, 5]
1  6  7  8  [9, 10]

I would like to create a filter using d, but normal comparison operations do not work:

>>> df['d'] == [4,5]
0    False
1    False
Name: d, dtype: bool

However, when I check line by line, I get what I expect

>>> df.loc[0,'d'] == [4,5]
True

What's going on here? How to perform list comparisons?

+4
source share
1 answer

This is a curious problem, probably due to the fact that the list is not hashed. I would go for an application:

df['d'].apply(lambda x: x == [4,5])

Of course, as suggested by DSM, the following works:

df = pd.DataFrame([[1,2,3,(4,5)],[6,7,8,(9,10)]], columns=['a','b','c','d'])
df['d'] == (4,5)
+3
source

All Articles