我在pandas中有以下数据框,我希望将其转换为JSON变量。以下是我拥有的数据示例:
DOCDATE GLPOSTDT CURTRXAM Customer_Code Reference Doc Type Status Customer_Name CRLMTAMT
0 2016-04-07 2016-04-07 525.88 ADHA 3859502 INV Active ADDO HARDWARE AND FURNISHERS 400000.00
1 2016-04-11 2016-04-11 9616.65 ADHA 3861622 INV Active ADDO HARDWARE AND FURNISHERS 400000.00
2 2016-04-28 2016-04-29 -5048.80 ADHA 0339942 PYM Active ADDO HARDWARE AND FURNISHERS 400000.00
3 2016-02-29 2016-02-29 -10462.36 CHMO 01728560 DED Legal CHECKIN SUPERMARKET 14500000.00
4 2016-04-20 2016-04-20 12727.87 CHMO 3871124 INV Legal CHECKIN SUPERMARKET 14500000.00
5 2016-04-20 2016-04-20 2640.92 CHMO 3871134 INV Legal CHECKIN SUPERMARKET 14500000.00
我正在寻找的输出如下:
Customer1 = {
"Customer_code" : "ADHA",
"Status" : "Active",
"CRLMTAMT" : "400000",
"Customer_Name" : "ADDO HARDWARE AND FURNISHERS",
"Transactions" : {
"INV" : {
"3859502" : {
"GLPOSTDT" : "2016-04-07",
"CURTRXAM" : "525.88"
},
"3861622" : {
"GLPOSTDT" : "2016-04-11",
"CURTRXAM" : "9616.65"
}
},
"PYM" : {
"0339942" : {
"GLPOSTDT" : "2016-04-29",
"CURTRXAM" : "-5048.80"
}
}
}
}
"交易' JSON变量的分支被分成不同的" Doc Types"。然后使用"参考"字段我会应用相应的" GLPOSTDT"和#34; CURTRXAM"。 创建第一个客户后,我会将其写入数据库,然后循环到下一个客户(Customer_code =" CHMO")但是我无法将每个Customer_code从pandas df转换/分组为JSON变量
答案 0 :(得分:0)
import json
dict_of_dataframe = dataframe.to_dict(orient='dict')
with open('result.json', 'w') as fp:
json.dump(dict_of_dataframe, fp)