我正在使用
pd.read_sql_query() 从数据库获取数据,然后使用
to_json(orient='records') 这是数据帧:
(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转换成这样的阵型。
[{
"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"
}]但我真正想要的应该是这样:
{
"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"
}
]
}我需要将相同的信息分组,并为其余的设置一个新的索引“篮子”。我怎么才能做到呢?非常感谢。
发布于 2018-07-26 13:14:34
对由difference、reset_index过滤的所有列使用带有to_dict的自定义函数的groupby,最后将其转换为to_json
cols = df.columns.difference(['price_formula_id','premium'])
j = (df.groupby(['price_formula_id','premium'])[cols]
.apply(lambda x: x.to_dict('r'))
.reset_index(name='basket')
.to_json(orient='records'))
print (j)
[{
"price_formula_id": 30064,
"premium": 0.0,
"basket": [{
"exchange": "CME",
"product_code": "CL",
"product_id": "c001",
"product_name": 2018,
"weight": 0.3
},
{
"exchange": "CME",
"product_code": "CL",
"product_id": "c002",
"product_name": 2018,
"weight": 0.7
}
]
}]https://stackoverflow.com/questions/51530653
复制相似问题