使用frozenset,它们可以用作字典的键(普通集合不可以)。
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 23 20:54:37 2017
@author: Rainey
"""
import pandas as pd
"""
user sku
1 111
1 222
2 333
2 444
2 555
3 666
4 777
"""
df = pd.read_csv('11.csv')
favorable_reviews_by_users = dict((k, frozenset(v.values)) for k, v in df.groupby("user")["sku"])
print(len(favorable_reviews_by_users))
print(favorable_reviews_by_users)
"""
4
{1: frozenset({222, 111}), 2: frozenset({555, 444, 333}), 3: frozenset({666}), 4: frozenset({777})}
"""