2

I have a dataframe

id    key
a1     1
a2     1
a3     1
a4     2
a5     2
a6     3

I want to create a dictionary with key as machine no, and id column as list

like:

{1: ['a1', 'a2', 'a3'], 2: ['a4', 'a5'], 3: ['a6']}

Can i use groupby first and then do .to_dict?

piRSquared
  • 285,575
  • 57
  • 475
  • 624
Shubham R
  • 7,382
  • 18
  • 53
  • 119

2 Answers2

3

I believe you need lists ad values of dict - use groupby + apply + to_dict:

d = df.groupby('key')['id'].apply(list).to_dict()
print (d)
{1: ['a1', 'a2', 'a3'], 2: ['a4', 'a5'], 3: ['a6']}

Or if need list with scalars add if/else to apply:

d = df.groupby('key')['id'].apply(lambda x: list(x) if len(x) > 1 else x.iat[0]).to_dict()
print (d)
{1: ['a1', 'a2', 'a3'], 2: ['a4', 'a5'], 3: 'a6'}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • this is great if we have one column, however, what if we have to get values from multiple columns? I came across a similar situation. I tried using this, but no success. I think `map` would do it, but not sure! If you are willing, I posted a question here: https://stackoverflow.com/questions/46623452/map-two-data-frames-to-create-a-dictionary-with-multiple-values-for-a-key-pand – i.n.n.m Oct 07 '17 at 18:45
1

Use a dictionary comprehension around the groupby iterator

{n: v.tolist() for n, v in df.groupby('key').id}
piRSquared
  • 285,575
  • 57
  • 475
  • 624