3

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.