Data transfer for vocabulary transformation

I have the following Dataframe:

     image          product  vp_fk               mask
0    136524         105       2316               51322
1    136524         105       2316               51324
2    136524         106       2316               51325
3    136524         106       2316               51328
4    136524         106       2316               51329
5    136525         108       2319               51330

I want to convert it to the following dictionary:

{105:[[136524, 2316, 51322], 
      [136524, 2316, 51324]]
106: [[136524, 2316, 51325], 
      [136524, 2316, 51328],
      [136524, 2316, 51329]]
108: [[136525, 2319, 51330]]}

Thanks for the help.

+4
source share
1 answer

try the following:

In [187]: df.groupby('product')[['image','vp_fk','mask']].apply(lambda grp: grp.values.tolist()).to_dict()
Out[187]:
{105: [[136524, 2316, 51322], [136524, 2316, 51324]],
 106: [[136524, 2316, 51325], [136524, 2316, 51328], [136524, 2316, 51329]],
 108: [[136525, 2319, 51330]]}
+4
source

Source: https://habr.com/ru/post/1666147/


All Articles