如何将几列熊猫数据框转换为另一条记录中的 JSON 记录?
Posted
技术标签:
【中文标题】如何将几列熊猫数据框转换为另一条记录中的 JSON 记录?【英文标题】:How to convert few columns of pandas dataframe into JSON record within another record? 【发布时间】: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,您会注意到差异。 【参考方案1】:我将修改json
输出,而不是在pandas
内。
从您的 json
数据开始
print(json.dumps(data, indent=4, sort_keys=True))
[
"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
]
以及您感兴趣的一些columns
的列表:
other_data = ["BANKL", "BANKN", "BANKS", "BEGRU", "BUKRS", "C_Block"]
你可以直接修改json
:
for i, record in enumerate(data):
data[i]['OtherData'] =
for key in list(record.keys()):
if key in other_data:
data[i]['OtherData'][key] = record[key]
del data[i][key]
print(json.dumps(data, indent=4, sort_keys=True))
[
"ADRNR": 2692629,
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"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":
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No"
,
"PSTLZ": null,
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
]
【讨论】:
感谢您的帮助。它确实奏效了。在将其转换为 JSON 之前,我一直在努力对其进行格式化。以上是关于如何将几列熊猫数据框转换为另一条记录中的 JSON 记录?的主要内容,如果未能解决你的问题,请参考以下文章