我有一个带有以下列的python熊猫数据框:
authResponseHeaders = ["Authorization"]
我想要这样的JSON格式的输出:
CUSTOMER_ID PRODUCT_ID VENDOR_ID DAT ORDER_ID COLOR_ID
0 10078229 508136536 450 2018-11-23 20183200576771 1000
1 10078229 508136532 450 2018-11-23 20183200576771 1000
2 10202280 506894206 450 2018-11-23 20183231461778 1000
3 10207584 500970872 2097 2018-11-23 20183231430937 1002
4 10207584 500970872 2097 2018-11-23 20183231430937 1000
5 10268028 511131122 450 2018-11-23 20183231418341 1000
6 10268028 509736876 450 2018-11-23 20183231418341 1000
7 10268028 507095754 450 2018-11-23 20183231418341 1000
8 10268028 513902792 450 2018-11-23 20183231418341 1000
9 10383692 508229004 450 2018-11-23 20183190670154 1000
我尝试过,但是从现在开始如果没有危险的串联,它不会成功。 这是我的代码:
[{
"CUSTOMER_ID": "10078229",
"PRODUCT": [{
"PRODUCT_ID": "508136536",
"VENDOR_ID": "450",
"DAT": "2018-11-23",
"ORDER_ID": "20183200576771",
"COLOR_ID": "1000",
"SIZE_ID": "1000"
}, {
"PRODUCT_ID": "508136532",
"VENDOR_ID": "450",
"DAT": "2018-11-23",
"ORDER_ID": "20183200576771",
"COLOR_ID": "1000",
"SIZE_ID": "1002"
}]
},
{
"CUSTOMER_ID": "10202280",
"PRODUCT": [{
"PRODUCT_ID": "506894206",
"VENDOR_ID": "450",
"DAT": "2018-11-23",
"ORDER_ID": "20183231461778",
"COLOR_ID": "1000",
"SIZE_ID": "1000"
}]
}
]
有没有一种使用函数的方法?
非常感谢
编辑:我喜欢这种形状的输出(3级JSON:客户,订单,(产品和供应商),您将如何做?
df_cre=pd.DataFrame()
ids=df_test["CUSTOMER_ID"].unique()
for i in ids:
df2=df_test[df_test["CUSTOMER_ID"]== i]
df2=df2.drop('CUSTOMER_ID',1)
js2="{\"CUSTOMER_ID\": \""+str(i)+"\",\"PRODUCTS\" :" + df2.to_json(orient='records', lines=False) + "}"
df_cre=df_cre.append(pd.DataFrame([[i,js2]], columns=('CUSTOMER_ID','KEY_EVENT')))
json_final='['
for row in df_cre.itertuples():
json_final+= row.KEY_EVENT +','
json_final=json_final[:-1]
json_final+= ']'
谢谢
答案 0 :(得分:0)
这将起作用:
print([{'CUSTOMER_ID ': x['CUSTOMER_ID'],
'PRODUCT': {k: v for k, v in x.items() if k != 'CUSTOMER_ID'}}
for x in df.to_dict('records')])
答案 1 :(得分:0)
result = [{"CUSTOMER_ID":name,"PRODUCT":group[['PRODUCT_ID','VENDOR_ID','DAT','ORDER_ID','COLOR_ID']].to_dict("records")} for name,group in df.groupby('CUSTOMER_ID')]
打印(结果),这会有所帮助。
答案 2 :(得分:0)
像这样吗?
df2 = df.groupby("CUSTOMER_ID")['PRODUCT_ID', 'VENDOR_ID', 'DAT', 'ORDER_ID','COLOR_ID'].apply(lambda x: x.to_dict(orient="records")).reset_index(name="PRODUCT").to_json(orient="records")
输出:
[
{
"CUSTOMER_ID": 10078229,
"PRODUCT": [
{
"PRODUCT_ID": 508136536,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183200576771,
"COLOR_ID": 1000
},
{
"PRODUCT_ID": 508136532,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183200576771,
"COLOR_ID": 1000
}
]
},
{
"CUSTOMER_ID": 10202280,
"PRODUCT": [
{
"PRODUCT_ID": 506894206,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183231461778,
"COLOR_ID": 1000
}
]
},
{
"CUSTOMER_ID": 10207584,
"PRODUCT": [
{
"PRODUCT_ID": 500970872,
"VENDOR_ID": 2097,
"DAT": "2018-11-23",
"ORDER_ID": 20183231430937,
"COLOR_ID": 1002
},
{
"PRODUCT_ID": 500970872,
"VENDOR_ID": 2097,
"DAT": "2018-11-23",
"ORDER_ID": 20183231430937,
"COLOR_ID": 1000
}
]
},
{
"CUSTOMER_ID": 10268028,
"PRODUCT": [
{
"PRODUCT_ID": 511131122,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183231418341,
"COLOR_ID": 1000
},
{
"PRODUCT_ID": 509736876,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183231418341,
"COLOR_ID": 1000
},
{
"PRODUCT_ID": 507095754,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183231418341,
"COLOR_ID": 1000
},
{
"PRODUCT_ID": 513902792,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183231418341,
"COLOR_ID": 1000
}
]
},
{
"CUSTOMER_ID": 10383692,
"PRODUCT": [
{
"PRODUCT_ID": 508229004,
"VENDOR_ID": 450,
"DAT": "2018-11-23",
"ORDER_ID": 20183190670154,
"COLOR_ID": 1000
}
]
}
]