简体   繁体   English

按预期将字典转换为 dataframe python 中的 output

[英]converting dictionary into dataframe as expected output in python

let's say i have a dictionary as假设我有一本字典

dj=  {
        "totalrecords": 2,
        "data": [
            {
                "stateCd": "U.K",
                "stateName": "uttarakhand",
                "details": {
                    "id": [
                        "2312-k",
                        "2312-k"
                    ],
                    "date": [
                        "10-OCT-2019",
                        "11-OCT-2019"
                    ],
                    "icp": [
                        2233,
                        6443
                    ],
                    "icpr": [
                        3.434,
                        23.232
                    ]
                }
            },
            {
                "stateCd": "U.P",
                "stateName": "uttar pradesh",
                "details": {
                    "id": [
                        "2712-k",
                        "5412-k"
                    ],
                    "date": [
                        "10-OCT-2019",
                        "11-OCT-2019"
                    ],
                    "icp": [
                        2233,
                        6443
                    ],
                    "icpr": [
                        32.434,
                        31.232
                    ]
                }
            }
        ]
    }
    } 

I want to convert this json/dictionary into data frame which would be like this using python:我想将此 json/字典转换为使用 python 的数据框:

在此处输入图像描述

but I am having no clue how to perform this action但我不知道如何执行此操作

i have also tried pandas.json_normalize() but didn't get my expected column in output ie date,icp,icpr我也尝试过pandas.json_normalize()但在 output 中没有得到我预期的列,即日期、icp、icpr

data_trunc=dj['data']
pd.json_normalize(data_trunc,record_path=['details','id'],meta=['stateCd','stateName'])

enter image description here在此处输入图像描述

You can try something like this你可以试试这样的

Reference 参考


data = [{'state': 'Florida',
             'shortname': 'FL',
             'info': {'governor': 'Rick Scott'},
             'counties': [{'name': 'Dade', 'population': 12345},
                                {'name': 'Broward', 'population': 40000},
                                {'name': 'Palm Beach', 'population': 60000}]},
            {'state': 'Ohio',
             'shortname': 'OH',
             'info': {'governor': 'John Kasich'},
             'counties': [{'name': 'Summit', 'population': 1234},
                                {'name': 'Cuyahoga', 'population': 1337}]}]
result = pd.json_normalize(data, 'counties', ['state', 'shortname',
                                               ['info', 'governor']])

Output: Output:

0        Dade       12345   Florida    FL    Rick Scott
1     Broward       40000   Florida    FL    Rick Scott
2  Palm Beach       60000   Florida    FL    Rick Scott
3      Summit        1234   Ohio       OH    John Kasich
4    Cuyahoga        1337   Ohio       OH    John Kasich

Maybe this can help you:-也许这可以帮助你: -

import json

f = open('1.json')
file = json.load(f)

for data in file['data']:
  stCd = data['stateCd']
  stateN = data['stateName']
  date = data['details']['date']
  icp = data['details']['icp']
  icpr = data['details']['icpr']
  full = f"StCd    StateName       Date          ICP          ICPR"
  whole = f"{stCd}     {stateN}    {date[0]}    {icp[0]}      {icpr[0]}"
  whole2 = f"{stCd}     {stateN}    {date[1]}    {icp[1]}      {icpr[1]}"
  print()
  print(full)
  print(whole)
  print(whole2)

Output:- Output:-

StCd    StateName       Date          ICP          ICPR
U.K     uttarakhand    10-OCT-2019    2233      3.434
U.K     uttarakhand    11-OCT-2019    6443      23.232

StCd    StateName       Date          ICP          ICPR
U.P     uttar pradesh    10-OCT-2019    2233      32.434
U.P     uttar pradesh    11-OCT-2019    6443      31.232

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM