如何将几列pandas dataframe转换为另一条记录中的JSON记录?

时间:2015-12-14 09:22:20

标签: json python-2.7 pandas

所以我试图将12+列的pandas Dataframe转换为相应的JSON Record。我能搞定。但是,我希望框架的几列成为新列的子记录。如何实现这一目标。

<ul class="sitemap">
{%- for item in sitemap recursive %}    
    <li><a href="{{ item.href|e }}">{{ item.title }}</a>
    {%- if item.children -%}
        <ul class="submenu">{{ loop(item.children) }}</ul>
    {%- endif %}</li>
{%- endfor %}
</ul>

以上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
               }

编辑:Flg是我的代码

 {

                "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
                }
           }

1 个答案:

答案 0 :(得分:0)

而不是在pandas内,我会修改json输出。

从您的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
    }
]