I have a dataframe df that looks like this for the first record for example:
oID mode flow custom s1 s2 plan1 plan2 prop1 prop2 rag eve1 eve2
0 AIR DD XWERTR1 29.03.2020 16.04.2020 29.03.2020 16.04.2020 29.03.2020 16.03.2020 G 16.03.2020 17.03.2020
1 SEA PP XWERTR1 07.04.2020 25.03.2020 07.04.2020 25.03.2020 07.04.2020 25.03.2020 A 16.03.2020 17.03.2020
2 AIR DD XWERTR1 04.04.2020 20.03.2020 04.04.2020 20.03.2020 04.04.2020 20.03.2020 G 16.03.2020 17.03.2020
3 AIR DD XWERTR1 04.04.2020 20.03.2020 04.04.2020 20.03.2020 04.04.2020 20.03.2020 G 16.03.2020 17.03.2020
4 SEA DD XWERTR1 04.04.2020 20.03.2020 04.04.2020 20.03.2020 04.04.2020 20.03.2020 R 16.03.2020 17.03.2020
I want to have json that looks like this:
{
"_id" : "0",
"oID" : "0",
"custom" : "XWERTR1",
"mode" : "AIR",
"flow" : "DD",
"milestone" : {
"creation" : {
"s1" : "29.03.2020",
"plan1" : "29.03.2020",
"prop1" : "29.03.2020"
},
"transp" : {
"s2" : "16.04.2020",
"plan2" : "16.04.2020",
"prop2" : "16.04.2020"
}
}
"rag" : "G",
"eve1" : "16.03.2020",
"eve2" : "17.03.2020"
}
Actually, under "milestone" newly created field, i'd create nested categories that are "creation" and "transp" . They will contain respectively the values of s1,plan1,prop1 and s2,plan2,prop2
I have this code that i took from here: Convert Pandas Dataframe to nested JSON But it's not sufficient to solve my problem as i want to add another array of element on Json (s2, plan2, prop2).
j = (df.groupby(['oID','mode','flow','custom','rag','eve1','eve2'], as_index=False)
.apply(lambda x: x[['s1','plan1','prop1']].to_dict('r'))
.reset_index()
.rename(columns={0:'creation'})
.to_json(orient='records',date_format='iso'))
i struggle to have "create" and the "transp" containing repectively the elements s1, plan1, prop1 and s2, plan2, prop2
How can i do this ?
Thanks