0

I have a dataframe as you can see in below:

enter image description here

Some pro1's have different pro2's.

I want to convert this dataframe to dictionary as:

bundles = {
    '12345' : ['a1','b1','c1','e1'],
    '56789' : ['a1'],
    '98765' : ['b1','d1']
}
Atakan
  • 45
  • 4

2 Answers2

1
bundles = df.groupby('pro1')['pro2'].apply(list).to_dict()

Output:

>>> bundles
{
    '12345': ['a1', 'b1', 'c1', 'e1'],
    '56789': ['a1'],
    '98765': ['b1', 'd1']
}
1

You can groupby on pro1 and create a list from pro2, finally convert to a dictionary:

out = df.groupby('pro1')['pro2'].agg(list).to_dict()
  • 2
    Per some benchmarks I'm running, `apply` is actually just a tiny bit faster than `agg`. –  Dec 11 '21 at 18:57