I have a dataframe that I need to convert to a dict and send via API as Json.
This is my df:
Category | Topic | Steps | Stud1 | Stud2 | Stud3 |
---|---|---|---|---|---|
Cat1 | topc1 | step1 | 10 | 15 | 30 |
Cat1 | topc2 | step2 | 16 | 26 | 42 |
Cat3 | topc3 | step3 | 05 | 62 | 50 |
I want to generate dict something like this:
{Cat1: {
topc1:{
step1:
[
{Stud1:10},
{Stud2:15},
{Stud3:30}
]
}
}
topc2:{
step2:
[
{Stud1:10},
{Stud2:15},
{Stud3:30}
]
}
}
}
I know I can achieve this with for loop and iterrows()
but that would be slow result.
I have tried using to_dict()
function from pandas with group_by()
on Category, Topic, Step but that doesn't give required output. I have tried many ways like stack()
and I even merging the rows of same name but it yields no result. Can anyone tell me best efficient way to achieve this.