I have a dataframe with a column with many different-sized lists.I would like to compare it with a second column preferably without expending a column_of_lists to multiple columns.
| value | column_of_lists |
| --- | --- |
| 1 | \[1,2,1,1\] |
| 3 | \[3\] |
| 1 | \[4,4\] |
| 3 | \[0\] |
to
value | is_in_column_of_lists |
---|---|
1 | True |
3 | True |
1 | False |
3 | False |
I tried
df.loc[df['value'].isin(df['column_of_lists']), 'is_in_column_of_lists'] = True
but I got
value | is_in_column_of_lists |
---|---|
1 | False |
3 | False |
1 | False |
3 | False |
>Solution :
Use df.apply
with in
operator:
df['is_booked'] = df.apply(lambda x: x['value'] in x['column_of_lists'], axis=1)
value column_of_lists is_booked
0 1 [1, 2, 1, 1] True
1 3 [3] True
2 1 [4, 4] False
3 3 [0] False