Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: how to compare a column to a column of lists

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading