I do not see any way that to_dict()
can create what you want here. The following solution is not the most Pythonic (or Pandanic), but it is a way to get what you want:
d={}
for pid,vid,v in df.itertuples(index=False):
d.setdefault(pid,[])
d[pid].append((vid,v))
The first line of the loop does nothing if a given patient_id
is already in the output dict, and adds an empty list if it is not. Then the second line appends the values you want to the empty list, or the existing list if it's already there.
EDIT: This answer also uses iteration and also speculates that pandas has no native way to do this. I've updated my answer it use itertuples()
which is a less-memory intensive method than my original as_matrix()
.