I have 2 columns in my dataframe
- product ID purchased by the customer "p"
list of products IDs purchased by similar customers "p_list"
df = pd.DataFrame({'p': [12, 4, 5, 6, 7, 7, 6,5],'p_list':[[12,1,5], [3,1],[8,9,11], [6,7,9], [7,1,2],[12,9,8], [6,1,15],[6,8,9,11]]})
I want to check if "p" exists on "p_list" or not, so I applied this code
df["exist"]= df.apply(lambda r: 1 if r["p"] in r["p_list"] else 0, axis=1)
The problem is that I have around 50 million rows in this dataframe, so it takes very long time to execute.
Is there more efficient way to compute this column?
Thanks.