将嵌套对象添加到Python中的现有逻辑

将嵌套对象添加到Python中的现有逻辑,python,python-3.x,Python,Python 3.x,下面是数据帧 df = pd.DataFrame([['xxx xxx','specs','67646546','TEST 123','United States of America']], columns = ['name', 'type', 'aim', 'aimd','context' ]) 我正在尝试在“数据”下添加对象“aimd”。 以下是格式 { "entities": [{ "name": "xxx xxx", "typ

下面是数据帧

df = pd.DataFrame([['xxx xxx','specs','67646546','TEST 123','United States of America']], columns = ['name', 'type', 'aim', 'aimd','context' ]) 
我正在尝试在“数据”下添加对象“aimd”。 以下是格式

{
    "entities": [{
            "name": "xxx xxx",
            "type": "specs",
            "data": {
                "attributes": {
                    "aimd": {
                        "values": [{
                                "value": "xxxxx",
                                "source": "internal",
                                "locale": "en_Us"
                            }
                        ]
                    }
                },
                "contexts": [{
                        "attributes": {
                            "aim": {
                                "values": [{
                                        "value": "67646546",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            }
                        },
                        "context": {
                            "country": "United States of America"
                        }
                    }
                ]
            }
        }
    ]
}

只需插入一个额外的数组:

import pandas as pd
import json

df = pd.DataFrame([['xxx xxx','specs','67646546','TEST 123','R12',43,'789S','XXX','SSSS','GGG','TTT','United States of America']], columns = ['name', 'type', 'aim', 'aimd','aim1','aim2','alim1','alim2','alim3','apim','asim','context' ]) 

exclude_list = ['name','type','aimd','context']


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':{},'contexts':[{'attributes':{},'context':{'country':row['context']}}]}}

        attr_row = row[~row.index.isin(['name','type'])]
        for idx2,row2 in attr_row.iteritems():
            if idx2 not in exclude_list:
                dict_temp = {}
                dict_temp[idx2] = {'values':[]}
                dict_temp[idx2]['values'].append({'value':row2,'source':'internal','locale':'en_Us'})
                temp_dict_alpha['data']['contexts'][0]['attributes'].update(dict_temp)

            if idx2 == 'aimd':
                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": "xxx xxx",
            "type": "specs",
            "data": {
                "attributes": {
                    "aimd": {
                        "values": [
                            {
                                "value": "TEST 123",
                                "source": "internal",
                                "locale": "en_Us"
                            }
                        ]
                    }
                },
                "contexts": [
                    {
                        "attributes": {
                            "aim": {
                                "values": [
                                    {
                                        "value": "67646546",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "aim1": {
                                "values": [
                                    {
                                        "value": "R12",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "aim2": {
                                "values": [
                                    {
                                        "value": 43,
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "alim1": {
                                "values": [
                                    {
                                        "value": "789S",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "alim2": {
                                "values": [
                                    {
                                        "value": "XXX",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "alim3": {
                                "values": [
                                    {
                                        "value": "SSSS",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "apim": {
                                "values": [
                                    {
                                        "value": "GGG",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            },
                            "asim": {
                                "values": [
                                    {
                                        "value": "TTT",
                                        "source": "internal",
                                        "locale": "en_Us"
                                    }
                                ]
                            }
                        },
                        "context": {
                            "country": "United States of America"
                        }
                    }
                ]
            }
        }
    ]
}

那么您面临的问题是什么呢?欢迎使用堆栈溢出!请编辑这篇文章,澄清你遇到的困难。请拿着这本书,读一读,你可能想读一读。非常感谢。在我的场景中,我的数据源有50多列,在上下文中,我需要的是在属性下只有一列,即aimd,这与您的逻辑完全正确,在上下文中,我将有50多列,我需要迭代所有列,在数据源下面我添加了5列。df=pd.DataFrame([[xxx-xxx','specs','67646546','TEST 123','R12',43','789S','xxx','SSSS','GGG','TTT','United of America']],columns=['name','type','aim','aimd','aim1','aim2','alim1','alim2','alim3','apim','asim','context'])上述df更新解决方案。