How to create dictionaries in a grouped format in pandas dataframe?

Question:

I have a data frame like this:

df:
ID   Award   Award_ID
1    Ninja    N13
1    Ninja    N19
1    Warrior  W16
2    Alpha    A99
2    Delta    D18
3    Alpha    A101
3    Alpha    A102
3    Alpha    A103

All these IDs are repeated in this data. For every ID, I want to create a dictionary with ID as key and Awards with its IDs as values in this format:

Output:
{
1:Ninja(N13/N19), Warrior(W16)
2:Alpha(A99), Delta(D18)
3:Alpha(A101,A102,A103)
}

Many Thanks!

Asked By: Yash

||

Answers:

Could you try this,

{k: f.groupby('Award')['Award_ID'].apply(tuple).to_dict()
     for k, f in df.groupby('ID')}

O/P:

 {1: {'Ninja': ('N13', 'N19'), 'Warrior': ('W16',)},
 2: {'Alpha': ('A99',), 'Delta': ('D18',)},
 3: {'Alpha': ('A101', 'A102', 'A103')}}
Answered By: Mohamed Thasin ah