11

I have a dataframe with 2 columns Address and ID. I want to merge IDs with the same addresses in a dictionary

import pandas as pd, numpy as np

df = pd.DataFrame({'Address' : ['12 A', '66 C', '10 B', '10 B', '12 A', '12 A'],
                'ID' : ['Aa', 'Bb', 'Cc', 'Dd', 'Ee', 'Ff']})
AS=df.set_index('Address')['ID'].to_dict()

print df

  Address  ID
0    12 A  Aa
1    66 C  Bb
2    10 B  Cc
3    10 B  Dd
4    12 A  Ee
5    12 A  Ff

print AS

{'66 C': 'Bb', '12 A': 'Ff', '10 B': 'Dd'}

What I want is for the duplicates to store multiple values like:

{'66 C': ['Bb'], '12 A': ['Aa','Ee','Ff'], '10 B': ['Cc','Dd']}

2 Answers 2

19

I think you can use groupby and a dictionary comprehension here:

>>> df
  Address  ID
0    12 A  Aa
1    66 C  Bb
2    10 B  Cc
3    10 B  Dd
4    12 A  Ee
5    12 A  Ff
>>> {k: list(v) for k,v in df.groupby("Address")["ID"]}
{'66 C': ['Bb'], '12 A': ['Aa', 'Ee', 'Ff'], '10 B': ['Cc', 'Dd']}
Sign up to request clarification or add additional context in comments.

1 Comment

What is I have multiple columns, I need df.groupby('Adress')['ID','XX']?
1

In response to the comment about multiple columns:

>>> df
  Address  ID  Name
0    12 A  Aa  Alpha
1    66 C  Bb  Bravo
2    10 B  Cc  Charlie
3    10 B  Dd  Delta
4    12 A  Ee  Edgar
5    12 A  Ff  Frank
>>> {k: v.to_dict() for k,v in df.groupby("Address")}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.