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 map, check if any values in a list is inside another

I have the following list

x  = [1,2,3]

And the following df

Sample df

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

pd.DataFrame({'UserId':[1,1,1,2,2,2,3,3,3,4,4,4],'Origins':[1,2,3,2,2,3,7,8,9,10,11,12]})

Lets say I want to return, the userid who contains any of the values in the list, in his groupby origins list.

Wanted result

pd.Series({'UserId':[1,2]})

What would be the best approach? To do this, maybe a groupby with a lambda, but I am having a little trouble formulating the condition.

>Solution :

df['UserId'][df['Origins'].isin(x)].drop_duplicates()

I had considered using unique(), but that returns a numpy array. Since you wanted a series, I went with drop_duplicates().

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