Is there an optimal way to do something like this?
Lets say I have the following DataFrame:
A B
0 1 1
1 1 2
2 2 3
3 2 4
4 2 5
I would like to get a dictionary like this:
{1: [1, 2], 2:[3, 4, 5]}
Keep in mind that the lists have different lengths because the value 1
appears two times and the value 2
appears three times. If I try
df.set_index('A').to_dic('list')
Pandas only keeps the last value in B for each value in A, returning the following dict:
{1:[2], 2:[5]