I am trying to create a dictionary from a dataframe where the first column value is the key and within it other columns use a combination of the header and value to create the dictionary.
import pandas as pd
data = [
[1,'name1', 'surname1'],
[2,'name2', 'surname2'],
[3,'name3', 'surname3']
]
df = pd.DataFrame(data,columns=['pkey','first_name', 'last_name'])
wanted_dictionary = {
1 : {'first_name' : 'name1', 'last_name' : 'surname1'},
2 : {'first_name' : 'name2', 'last_name' : 'surname2'},
3 : {'first_name' : 'name3', 'last_name' : 'surname3'},
}
print(wanted_dictionary)
I have tried many variations using to_dict and groupby but just can't seem to crack it.
CodePudding user response:
Use set_index
followed by to_dict
:
res = df.set_index("pkey").to_dict("index")
print(res)
Output
{1: {'first_name': 'name1', 'last_name': 'surname1'},
2: {'first_name': 'name2', 'last_name': 'surname2'},
3: {'first_name': 'name3', 'last_name': 'surname3'}}
CodePudding user response:
You can use:
df.set_index("pkey").to_dict(orient="index"))
This outputs:
{
"1": {
"first_name": "name1",
"last_name": "surname1"
},
"2": {
"first_name": "name2",
"last_name": "surname2"
},
"3": {
"first_name": "name3",
"last_name": "surname3"
}
}