I have a pandas Data Frame like below :
v_id | r_id | count |
---|---|---|
222 | 1 | 1000 |
222 | 2 | 1500 |
222 | 9 | 600 |
33 | 1 | 7000 |
555 | 1 | 6000 |
555 | 5 | 60 |
555 | 7 | 2300 |
how can I convert it to into the following new table :
v_id | data |
---|---|
222 | { 2 : 1500 , 1 : 1000 , 9 : 600 } |
33 | { 1 : 7000 } |
555 | { 1 : 6000 , 7 : 2300 , 5 : 60 } |
Please note that the values of the dictionary are in descending order.
The dataset has 5000 values, I haven't figured out a solution yet. Any help is highly appreciated. Thank you.
set the index and use groupby
df.set_index('r_id').groupby('v_id')['count'].agg(dict)
v_id
33 {1: 7000}
222 {1: 1000, 2: 1500, 9: 600}
555 {1: 6000, 5: 60, 7: 2300}
Name: count, dtype: object