Vamsi nimmala
Vamsi nimmala

Reputation: 59

Convert dataframe into dictionary containing list of dictionaries

My dataframe is as shown

 name    key    value
 john    A223   390309
 jason   B439   230943
 peter   A5388  572039
 john    D23902 238939
 jason   F2390   23930

I want to convert the above generated dataframe into a nested dictionary with list of dictionary in the below shown format.

{'john': [{'key':'A223', 'value':'390309'}, {'key':'A5388', 'value':'572039'}],
 'jason': [{'key':'B439','value':'230943', {'key':'F2390', 'value'2:'23930'}],
 'peter': [{'key':'A5388'  ,'value':'572039'}]}

could some one help with this.

Upvotes: 3

Views: 96

Answers (3)

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

try this,

final_dict={}
def dict_make(row):
    m_k= row['name'].values.tolist()[0]
    final_dict[m_k]=  row.set_index('name').to_dict(orient='records')
df.groupby('name').apply(dict_make)
print final_dict

Output:

{'peter': [{'value': 572039, 'key': 'A5388'}], 
'john': [{'value': 390309, 'key': 'A223'}, {'value': 238939, 'key': 'D23902'}],
'jason': [{'value': 230943, 'key': 'B439'}, {'value': 23930, 'key': 'F2390'}]}

Upvotes: 0

dataista
dataista

Reputation: 3457

You can use groupby, apply, iterrows and Series' tolist as below:

def f(rows):
      return {rows.iloc[0]['name']: [{'key': row['key'], 'value': row['value']} for _, row in rows.iterrows()]}

df.groupby("name").apply(f).tolist()

Generating the results you want:

[{'jason': [{'key': 'B439', 'value': '230943'}, {'key': 'F2390', 'value': '23930'}]},
 {'john': [{'key': 'A223', 'value': '390309'}, {'key': 'D23902', 'value': '238939'}]},
 {'peter': [{'key': 'A5388', 'value': '572039'}]}]

Explanation:

  • With groupby("name") we aggregate all the rows per name
  • Then we are applying the function f to each of those groups of rows with apply(f)
  • f iterates through those rows with iterrows creating a list of dictionaries with [{'key': row['key'], 'value': row['value']} for _, row in rows.iterrows()] and finally we take just the first row's name with rows.iloc[0]['name'] to create the final dictionary for this name.
  • We aggregate all the dictionaries per name with tolist()

Upvotes: 1

Space Impact
Space Impact

Reputation: 13255

Use dictionary comprehension with to_dict:

d = {name:df.loc[df.name==name,['key','value']].to_dict('records') for name in df.name.unique()}

print(d)
{'john': [{'key': 'A223', 'value': 390309}, {'key': 'D23902', 'value': 238939}], 
 'jason': [{'key': 'B439', 'value': 230943}, {'key': 'F2390', 'value': 23930}], 
 'peter': [{'key': 'A5388', 'value': 572039}]}

Upvotes: 1

Related Questions