Ryan Wilkinson
Ryan Wilkinson

Reputation: 37

How can I create a dictionary of dictionaries of Unique Key Pairs?

I have a pandas dataframe with columns IssuerId and Sedol. The IssuerIds will be the same in some instances but the Sedol will always be different.

I would like to create a dictionary or multi-level index that aggregates these such that I can easily traverse them. For instance I currently have:

IssuerID Sedol
1          1
1          2
2          3
3          4

And I want to somehow create:

[{1: [1,2]},{2: 3},{3:4}]

Upvotes: 1

Views: 60

Answers (2)

BENY
BENY

Reputation: 323326

Using groupby

l=[{x:y.tolist()}for x , y in df.groupby('IssuerID')['Sedol']]
l
[{1: [1, 2]}, {2: [3]}, {3: [4]}]

Upvotes: 1

pault
pault

Reputation: 43524

If you do groupBy + apply(list) and then call to_dict, you get:

d = df.groupby("IssuerID")["Sedol"].apply(list).to_dict()
print(d)
#{1: [1, 2], 2: [3], 3: [4]}

Now just reformat d to get your desired output.

If you want a dictionary, use a dict comprehension:

new_dict = {k: v if len(v) > 1 else v[0] for k, v in d.items()}
print(new_dict)
#{1: [1, 2], 2: 3, 3: 4}

If you want a list of dictionaries, use a list comprehension:

new_list = [{k: v if len(v) > 1 else v[0]} for k, v in d.items()]
print(new_list )
#[{1: [1, 2]}, {2: 3}, {3: 4}]

Upvotes: 1

Related Questions