I do not know if there is a function available in Pandasto perform this task. However, I tried to make a workaround to solve your problem.
Here is the complete code.
import pandas as pd
dictionary = {'userid':[1,1,1,1,2,2,2],
'itemid':[1,1,3,4,1,2,3]}
df = pd.DataFrame(dictionary, columns=['userid', 'itemid'])
selected_user = []
for user in df['userid'].drop_duplicates().tolist():
items = df.loc[df['userid']==user]['itemid'].tolist()
if len(items) != len(set(items)): continue
else: selected_user.append(user)
result = df.loc[(df['userid'].isin(selected_user))]
This code will produce the following result.
userid itemid
4 2 1
5 2 2
6 2 3
Hope this helps.
source
share