I need to get this result. the column funz_id and funz_desc in a new column to dict.
soc_id funz_id funz_desc funz_dict
1 6410000000 Direzione {'funz_id': '6410000000', 'funz_desc': 'Direzione }
1 6410000200 DG-GM {'funz_id': '6410000200', 'funz_desc': 'DG-GM }
i try:
df['funz_dict'] = {'funz_id': df['funz_id'], 'funz_desc': df['funz_desc']}
but i get error <class 'ValueError'> - Length of values (2) does not match length of index (89)
CodePudding user response:
You can use .to_dict(orient='records')
:
df[['funz_id', 'funz_desc']].to_dict(orient='records')
returns a list of dictionaries, one dictionary for each record/row:
[{'funz_id': 6410000000, 'funz_desc': 'Direzione'},
{'funz_id': 6410000200, 'funz_desc': 'DG-GM'}]
So you can add a new column using:
df['funz_dict'] = df[['funz_id', 'funz_desc']].to_dict(orient='records')