I have this data frame which I’d like to convert to a dict in python, I have many other categories, but showed just two for simplicity
Category Name Description Price Diesel Land Rover No Description found £ x Electric Tesla Model X No Description found £ x
I want the output to be like this
dict = {"Category": {"Diesel" : {
"Name": "Land Rover",
"Description":"No Description Found",
"Price": "£ x" },
"Electric" : {"Name": "Tesla Model X",
"Description":"No Description Found",
"Price": "£ x" }
}
}
Advertisement
Answer
You can do this without assigning an additional column or aggregating using list:
def collect(category):
return category[['Name', 'Description', 'Price']].to_dict('records')
data = {'Category': df.groupby('Category').apply(collect).to_dict()}
I created a separate function for readability – you could, of course, pass it as a lambda too:
{
'Category': df.groupby('Category').apply(
lambda x: x[['Name', 'Description', 'Price']].to_dict('records')).to_dict()
}