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

How to compare 2 columns of a dataframe values with possibilities that are in a list

I would like to compare 2 columns of a dataframe values with possibilities that are in a list.

I have this list :

lst=[[1, 1], [2, 1], [4, 1], [6, 2]] #[Poteau,depart]

and this dataframe :

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

data = {'poteau': [2, 6, 3, 4], 'depart': [1, 2, 3, 1]}
df = pd.DataFrame(data)

enter image description here

I’m expecting this kind of result :

enter image description here

I tried with df.isin() but it does not work as expected. Does somebody have an idea ?
Thanks !

>Solution :

You could merge with indicator and then filter:

merged = df.merge(pd.DataFrame(lst, columns=df.columns),how="left",indicator="Exist")
output = merged["Exist"].eq("both")

>>> output
0     True
1    False
2    False
3     True
Name: Exist, dtype: bool
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