I am using
pd.read_sql_query()
to get data from database and then use
to_json(orient='records')
this is the dataframe:
(1)
price_formula_id premium product_id exchange product_name product_code weight
0 30064 0.0 c001 CME 2018 CL 0.3
1 30064 0.0 c002 CME 2018 CL 0.7
(2)
price_formula_id premium product_id exchange product_name product_code weight
0 30064 NONE c001 CME 2018 CL 0.3
1 30064 NONE c002 CME 2018 CL 0.7
to convert to this formation.
[{
"price_formula_id": "30064",
"premium": "0.0",
"product_id": "c001",
"exchange": "CME",
"product_name": "2018",
"product_code": "CL",
"weight": "0.3"
},
{
"price_formula_id": "30064",
"premium": "0.0",
"product_id": "c002",
"exchange": "CME",
"product_name": "2018",
"product_code": "CL",
"weight": "0.7"
}]
but what I really want should be like this :
{
"price_formula_id": "30064",
"premium": "0.0",
"basket":
[
{"product_id": "c001",
"exchange": "CME",
"product_name": "2018",
"product_code": "CL",
"weight": "0.3"
},
{
"product_id": "c002",
"exchange": "CME",
"product_name": "2018",
"product_code": "CL",
"weight": "0.7"
}
]
}
I need to group the same info and set a new index 'basket' for the rest.
how could I make it?
Thanks very much.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…