still trying to think through how to describe this properly (will update question), but here’s my have/want minimal, reproducable, example of what I’m trying to do.
have = pd.DataFrame({'id': [1,1,1,2,2], 'grp': ['a', 'b', 'c', 'd', 'e'], 'val': [5,4,3,2,1]})
>>> have
id grp val
0 1 a 5
1 1 b 4
2 1 c 3
3 2 d 2
4 2 e 1
want = pd.DataFrame({'id': [1,2], 'results': [[('a', 5), ('b', '4'), ('c', 3)], [('d',2), ('e',1)]]})
>>> want
id results
0 1 [(a, 5), (b, 4), (c, 3)]
1 2 [(d, 2), (e, 1)]
>Solution :
You can try groupby id column then zip the grp and val columns
out = (have.groupby('id')
.apply(lambda g: list(zip(g['grp'], g['val'])))
.rename('result')
.reset_index())
print(out)
id result
0 1 [(a, 5), (b, 4), (c, 3)]
1 2 [(d, 2), (e, 1)]