I have two dataframes, one has a column that contains a list of values and the other one has some values.
I want to filter the main df if one of the values in the second df exists in the main df column.
Code:
import pandas as pd
A = pd.DataFrame({'index':[0,1,2,3,4], 'vals':[[1,2],[5,4],[7,1,26],['-'],[9,8,5]]})
B = pd.DataFrame({'index':[4,7], 'val':[1,8]})
print(A)
print(B)
print(B['val'].isin(A['vals'])) # Will not work since its comparing element to list
result = pd.DataFrame({'index':[0,2,4], 'vals':[[1,2],[7,1,26],[9,8,5]]})
Dataframe A
| index | vals |
|---|---|
| 0 | [1, 2] |
| 1 | [5, 4] |
| 2 | [7, 1, 26] |
| 3 | [-] |
| 4 | [9, 8, 5] |
Dataframe B
| index | val |
|---|---|
| 4 | 1 |
| 7 | 8 |
Result
| index | vals |
|---|---|
| 0 | [1, 2] |
| 2 | [7, 1, 26] |
| 4 | [9, 8, 5] |