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

标签 python python-3.x

下面是数据框

df = pd.DataFrame([['xxx xxx','specs','67646546','TEST 123','United States of America']], columns = ['name', 'type', 'aim', 'aimd','context' ]) 

我正在尝试在“data”下添加一个对象“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"
                        }
                    }
                ]
            }
        }
    ]
}

关于python - 将嵌套对象添加到 Python 中的现有逻辑中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185178/

相关文章:

python - 如何从pandas中的csv文件中进行计数和百分比

python - 有没有办法在Python中使用Selenium获取Web元素的outerHTML的outerHTML?

python - 如何禁用 PyQt5 中的右键单击和小部件问题

mysql - 尝试将记录从 CSV 加载到 mysql

python - 将默认 python 版本从 2.6.6 更改为 2.7 并配置 django

python - numpy all 不同于 builtin all

python - BeautifulSoup4 with Python3 - 如何使用规则将输出数据分离并写入不同的文件中?

python - 如何一次存储和删除正则表达式模式?

python - 定义 __new__ 后对象不带任何参数

python - turtle 图形在自身上绘制