首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对子DataFrame重复DataFrame行

对子DataFrame重复DataFrame行
EN

Stack Overflow用户
提问于 2020-09-10 11:38:36
回答 1查看 42关注 0票数 1

源代码是一个包含嵌套字典的JSON文件。

我创建了一个顶层defaultdict(dict)和一个for循环来获取第1到7行、column State、Size、Pop的数据帧。

在上面的for循环中,我再次创建了另一个(子) defaultdict(dict) and for循环,以获取第1行到第2行、column City、Size、Pop的数据帧

我将孩子defaultdict(dict)附加到最上面的defaultdict(Dict)

父数据框中的行应对子DataFrame重复

期望输出

代码语言:javascript
复制
    State   Size    Pop   City      Size    Pop
1    MH     120     300    MU        100    150
2    MH     120     300    PU        80     110
3    MH     120     300    NG        75     120
4    MH     120     300    PC        85     110
5    GJ     110     250    SU        70     100
6    GJ     110     250    VA        75     80
7    GJ     110     250    AH        85     120

另一个使用输入JSON的示例

输入JSON:

代码语言:javascript
复制
{
    "datatop": [
        {
            "datastate": {
                "attributes": {
                    "Name": "ABC",
                    "Place": "123"
                },
                "children": [
                    {
                        "datacity": {
                            "attributes": {
                                "CName": "EFG",
                                "CPlace": "12345"
                            }
                        }
                    },
                    {
                        "datacity": {
                            "attributes": {
                                "CNAME": "HIJ",
                                "CPlace": "6789"
                            }
                        }
                    }
                ]
            }
        },
        {
            "datastate": {
                "attributes": {
                    "Name": "XYZ",
                    "Place": "456"
                },
                "children": [
                    {
                        "datacity": {
                            "attributes": {
                                "CName": "LMN",
                                "CPlace": "1123"
                            }
                        }
                    },
                    {
                        "datacity": {
                            "attributes": {
                                "CName": "OPQ",
                                "CPlace": "22345"
                            }
                        }
                    }
                ]
            }
        }
    ],
    "totalCount": "2"
}

预期输出:

代码语言:javascript
复制
Name    Place   CName   CPlace
ABC     123     EFG     12345
ABC     123     HIJ     6798
XYZ     456     LMN     1123
XYZ     456     OPQ     22345
EN

回答 1

Stack Overflow用户

发布于 2020-09-12 18:36:47

通过改变我的方法,我能够达到预期的输出。

早些时候,我尝试对子for循环中的数据框中的行重复父数据框中的行(我从父for循环中获得的行)。

新方法:在父for循环之前创建一个空列表。从父for循环到子for循环编排键值对,并创建一个由来自父循环的键值对和来自子(嵌套) for循环的键值对组成的大字典。为了制作唯一的键,我使用了父循环和子循环的索引的字符串连接。子for循环结束后,在其外部(在父for循环的末尾),我将字典转换为data frame并将其附加到列表中。一旦父for循环完成,列表中的数据帧就完整了。最后,我对列表中的数据帧进行了连接。

代码语言:javascript
复制
import json
import pandas as pd
from collections import defaultdict

with open("./input.json") as file:
    filedata = json.load(file)

data = filedata['datatop']

dflist = []

for i in range (len(data)):

    attribute = (data[i])['datastate']['attributes']
    child = (((data[i]['datastate'])['children']))

    def_dct_chd = defaultdict(dict)

    # For Loop to Iterate over all the subnets of the L3OUT EPG
    for j in range (len(child)):
        def_dct_chd[str(i)+str(j)]['Name'] = attribute['Name']
        def_dct_chd[str(i)+str(j)]['Place'] = attribute['Place']
        def_dct_chd[str(i)+str(j)]['CName'] = child[j]['datacity']['attributes']['CName']
        def_dct_chd[str(i)+str(j)]['CPlace'] = child[j]['datacity']['attributes']['CPlace']

    # Create the Data Frames out of the dictionary and append it to the list
    dflist.append(pd.DataFrame(def_dct_chd).T)

# concatenate all the dictionaries inside the list
finaldf = pd.concat(dflist)

finaldf = finaldf.reset_index(drop=True)
print(finaldf)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63822480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档