1

I have created a data frame having column name as feature_id and tag_id.


feature_id = [1326, 1326, 1326, 1327, 1327, 1328, 1328]
  
tag_id = [2468, 2415, 2589, 2419, 2439, 2580, 2689]
  
list_tuples = list(zip(feature_id,tag_id))  
  
df = pd.DataFrame(list_tuples, columns=['feature_id', 'tag_id'])  
df

I got this dataframe:

    feature_id  tag_id
0   1326    2468
1   1326    2415
2   1326    2589
3   1327    2419
4   1327    2439
5   1328    2580
6   1328    2689

I need a dictionary with list of tag_id.

I am trying this:

data_dict = defaultdict(int)
data_dict = df.reset_index().to_dict(orient='list')

But I need the output as below:

{1326: [2468, 2415, 2589], 1327: [2419, 2439], 1328: [2580, 2689]}


richdotcom
  • 13
  • 4

0 Answers0