Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues using compare lists in pandas DataFrame

Tags:

python

pandas

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

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'd like to build a filter using d, but the normal comparison operations don't seem to work:

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

However when I inspect row by row, I get what I would expect

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

What's going on here? How can I do list comparisons?

like image 227
Mike Avatar asked Sep 29 '22 23:09

Mike


1 Answers

It is a curious issue, it probably has to do with the fact that list are not hashable I would go for apply:

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)

Another solution is use list comprehension:

df[[x == [4, 5] for v in df['col2']]]
like image 163
Gioelelm Avatar answered Oct 03 '22 07:10

Gioelelm