【发布时间】:2016-03-19 17:10:27
【问题描述】:
所以我正在尝试将具有 12 列以上的 pandas 数据框转换为相应的 JSON 记录。我能得到它。但是,我希望框架的几列成为新列的子记录。这怎么能实现。?
{
"ADRNR": 2692629,
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No",
"KOINH": null,
"LAND1": "US",
"LOEVM_x": null,
"LOEVM_y": null,
"MasterDataID": "10099",
"MasterDataType": "Vendor",
"NAME1": "LEGAL",
"NODEL_x": null,
"NODEL_y": null,
"ORT01": null,
"OtherData": null,
"PSTLZ": null,
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
}
上面的 JSON 是我得到的。但我想要以下结构。请指导我。
{
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"MasterDataID": "10099",
"MasterDataType": "Vendor",
"OtherData": {
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No",
"KOINH": null,
"LAND1": "US",
"LOEVM_x": null,
"LOEVM_y": null,
"NAME1": "LEGAL",
"NODEL_x": null,
"NODEL_y": null,
"ORT01": null,
"PSTLZ": null,
"ADRNR": 2692629,
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
}
}
编辑:Flg 是我的代码
Final_Table['AlertID'] = ''
Final_Table['AlertTimestamp'] = datetime.now().strftime('%d-%m-%Y %H:%M:%S')
Final_Table['MasterDataType'] = 'Vendor'
Final_Table['RuleID'] = 'Rule3'
Final_Table['RuleName'] = 'Vendor and Bank Country is Different'
Final_Table = Final_Table.rename(columns={'LIFNR': 'MasterDataID'})
Result = Final_Table[Final_Table['BANKS'] != Final_Table['LAND1']]
Result['OtherData'] = np.NaN
final_result = {'alerts': json.loads(Result.to_json(orient = 'records',force_ascii = False).encode('utf8'))}
result = {'results': final_result}
with open('output_Rule3.json', 'w') as outfile:
json.dump(result, outfile, indent = 5, sort_keys = True)
log.info("Rule3 : Execution Successful")
【问题讨论】:
-
你能展示你的努力吗
-
@EdChum 添加了代码!
-
你想要的和你得到的有什么区别?它们看起来很相似。
-
@JohnZwinck 我想要几列进入“OtherData”。如果您比较两个 JSON 中的 OtherData,您会注意到差异。
标签: json python-2.7 pandas