user5779223 user5779223 - 1 month ago 9
Python Question

How to apply a function can test whether an element in a list in data frame with python-pandas?

For a data frame

df
, it has a column
col
contains
list
value:

id col
1 [1, 10, 23]
2 [2, 11, 19, 29]
..


I tried:

df[1 in df.col]


But got an error:

KeyError: True


Do you know how can I implement it appropriately? Thanks in advance!

Answer

option using apply
df.col.apply(lambda x: 1 in x)

demo
df[df.col.apply(lambda x: 1 in x)]
enter image description here

Comments