简体   繁体   中英

From DataFrame to Nested Json object

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

After some times i could solved the issue using the template method:

d = """{
    "Ord" : "%s",
    "MOT" : "%s",
    "MVT" : "%s",
    "CUST" : "%s",
    "milestone" : {
        "creation" : {
            "sla" : "%s",
            "plan" : "%s",
            "proposed" : "%s"
        },
        "Pickup" : {
            "sla" : "%s",
            "plan" : "%s",
            "proposed" : "%s"
        }
    }
}
"""
js = []

for item in df.values:
    js.append(json.loads(d%tuple(item.tolist())))

print(json.dumps(js))

Thanks to @PeterMortesen and RaminNietzsche for helping out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM