Here is my problem, where I want to find element of column A in list of elements of column B of a dataframe. As a result I want to to only keep those rows, where the element in A was found:
df = pd.DataFrame({'A': [1, 2],
'B': [1, 3]
})
result = df[df.A.isin(df.B)]
>>> result
A B
0 1 1
works fine, but what I really want is:
df = pd.DataFrame({'A': [1, 2],
'B': [[1, 2], [1, 3]]
})
result = df[df.A.isin(df.B)]
>>> result
Empty DataFrame
Columns: [A, B]
Index: []
Which does not work as the elements from A are not compared with the elements of the lists in column B but with the whole list?
What I would like to have as a result is:
>>> result
A B
0 1 [1, 2]
Is that possible?
You can do
apply
:or zip comprehension:
output: