Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import pandas as pd

data = pd.DataFrame([[11, "a1", "b1"], [11, "a2", "b1"], [11, "a3", "b1"], [12, "c1", "d1"]], columns=["idx", "r1", "r2"])
data = data.set_index('idx')

def process_grp(grp):
    return {k: list(set(v)) for k,v in grp.to_dict(orient='list').items()}

result = {key: process_grp(grp) for key, grp in data.groupby(level=0)}

print(result)

# output
# {
#   11: {r1: [a1, a2, a3], 
#        r2: [b1]}, 
#   12: {r1: [c1], r2: [d1]}
# }