Reputation: 37
I have a pandas dataframe with columns IssuerId
and Sedol
. The IssuerId
s 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
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
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