Search code examples
pythonpandaslistdataframeseries

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

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().