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?
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']]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With