使用Pandas将CSV转换为嵌套JSON复杂结构

时间:2019-08-14 04:55:21

标签: python json pandas nested jupyter-notebook

使用熊猫转换为嵌套的JSON文件

这是一行的示例csv

var fixparser = require("fixparser");
const fixParser = new FIXParser();

fixParser.connect({
host: HOST,
port: PORT,
protocol: 'tcp',
sender: SENDER,
target: TARGET,
fixVersion: VERSION
});
// Sendlogon function
function sendLogon() {
const logon = fixParser.createMessage(
    new Field(Fields.MsgType, Messages.Logon),
    new Field(Fields.MsgSeqNum, fixParser.getNextTargetMsgSeqNum()),
    new Field(Fields.SenderCompID, SENDER),
    new Field(Fields.SendingTime, fixParser.getTimestamp()),
    new Field(Fields.TargetCompID, TARGET),
    new Field(Fields.ResetSeqNumFlag, 'Y'),
    new Field(Fields.EncryptMethod, EncryptMethod.None),
    new Field(Fields.HeartBtInt, 10)
);
const messages = fixParser.parse(logon.encode());

fixParser.send(logon);
}
// Open connection
fixParser.on('open', async (s, se) => {
    console.log("Started....");    
    sendLogon();
});
// Retrive response
fixParser.on('message', (message) => {
// Received FIX message
// console.log("message",message);
console.log('received message', message.description, message.string);
});
// Close connection
fixParser.on('close', () => {
console.log("closed");
});

我正在尝试为每行实现以下嵌套JSON结构

1 个答案:

答案 0 :(得分:0)

import pandas as pd
import json

df = pd.DataFrame([['specs','glass','70072187','ESA65Z45','ESA 65Z45','CUT TIP FG 1808-40'],
                   ['specs','glass','666','ESA6665','ESB 666','CUT TIP FG 66-40']],
                       columns = ['name', 'type','aitm','alitm','aaitm','adsc1' ])


data = {'entities':[]}
for key,grp in df.groupby('name'):
    for idx, row in grp.iterrows():
        temp_dict_alpha = {'name':key, 'type':row['type'], 'data':{'attributes':{}}}

        attr_row = row[~row.index.isin(['name','type'])]
        for idx2, row2 in attr_row.iteritems():
            dict_temp = {}
            dict_temp[idx2] = {'values':[]}
            dict_temp[idx2]['values'].append({'value':row2,'source':'internal','locale':'en_US'})

            temp_dict_alpha['data']['attributes'].update(dict_temp)

        data['entities'].append(temp_dict_alpha)


print(json.dumps(data, indent= 4))   

输出:

print(json.dumps(data, indent= 4))
{
    "entities": [
        {
            "name": "specs",
            "type": "glass",
            "data": {
                "attributes": {
                    "aitm": {
                        "values": [
                            {
                                "value": "70072187",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "alitm": {
                        "values": [
                            {
                                "value": "ESA65Z45",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "aaitm": {
                        "values": [
                            {
                                "value": "ESA 65Z45",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "adsc1": {
                        "values": [
                            {
                                "value": "CUT TIP FG 1808-40",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    }
                }
            }
        },
        {
            "name": "specs",
            "type": "glass",
            "data": {
                "attributes": {
                    "aitm": {
                        "values": [
                            {
                                "value": "666",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "alitm": {
                        "values": [
                            {
                                "value": "ESA6665",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "aaitm": {
                        "values": [
                            {
                                "value": "ESB 666",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    },
                    "adsc1": {
                        "values": [
                            {
                                "value": "CUT TIP FG 66-40",
                                "source": "internal",
                                "locale": "en_US"
                            }
                        ]
                    }
                }
            }
        }
    ]
}