Filter pandas dataframe rows if any value on a list inside the dataframe is in another list
You can convert each list to sets, get intersection and convert to bool:
L = [480, 9, 104]
mask = np.array([bool(set(map(int, x)) & set(L)) for x in df['split_categories']])
Or convert list column
to DataFrame
, cast to float and compare with isin
:
df1 = pd.DataFrame(df['split_categories'].values.tolist(), index=df.index)
mask = df1.astype(float).isin(L).any(axis=1)
df = df[mask]
print (df)
album_id categories split_categories
0 66562 480.494 [480, 494]
3 1709 9 [9]
4 59239 105.104 [105, 104]
You can expand the inner list, and check if any
items in the inner lists are contained in [480, 9, 104]
:
l = [480, 9, 104]
df[df.categories.str.split('.', expand=True).isin(map(str,l)).any(axis=1)]
album_id categories split_categories
0 66562 480.494 [480,494]
3 1709 9.000 [9]
4 59239 105.104 [105,104]