使用相同的键值从json文件中提取数据帧

时间:2018-03-22 19:35:11

标签: python json pandas dictionary dataframe

我有一个json文件(如下所示),我需要在配置文件数据帧,其列名为“jive_label”的值> []和值作为其值,预期输出如下。我也给了示例代码,我尝试只打印一列,但该代码显然无效。

import requests
import pandas as pd
import numpy as np
from pandas.io.json import json_normalize
import sys
import json


r=requests.get(url,data=payload,headers=headers,params=querystring,verify=False)
json_data=r.json()
df=pd.DataFrame([])
    for i in json_data.get('profile',[]):
        if i.get('jive_label')=='Title':

            dfDict={'Title': i.get('value')
                   }
         df=df.append(pd.DataFrame(dfDICT,index=[0]),ignore_index=True)
    print(df.head())*


  {
        "jive": {
                "enabled": true,
                "external": false,
                "federated": true,
                "lastProfileUpdate": "2017-08-14T17:07:35.491+0000",
                "level": {
                    "description": "Level 1",
                    "imageURI": "https:",
                    "name": "Newbie",
                    "points": 0
                },
                "locale": "en_US",
                "externalContributor": false,
                "profile": [
                    {
                        "jive_label": "Title",
                        "value": "Analyst",
                        "jive_displayOrder": 0,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "COMPANY ID",
                        "value": "333333",
                        "jive_displayOrder": 5,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Department",
                        "value": "46152",
                        "jive_displayOrder": 6,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "BUFUGU",
                        "value": "C06",
                        "jive_displayOrder": 9,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "XYZ Company Code",
                        "value": "DA01",
                        "jive_displayOrder": 10,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Business Purpose",
                        "value": "C0820",
                        "jive_displayOrder": 11,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Company",
                        "value": "XYZ",
                        "jive_displayOrder": 19,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Street Address",
                        "value": "30 NY NY",
                        "jive_displayOrder": 20,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "City",
                        "value": "NYC",
                        "jive_displayOrder": 21,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Province/State",
                        "value": "NY",
                        "jive_displayOrder": 22,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Postal/Zip Code",
                        "value": "00000",
                        "jive_displayOrder": 23,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Country",
                        "value": "United States",
                        "jive_displayOrder": 24,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Preferred Language",
                        "value": "E",
                        "jive_displayOrder": 30,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Display Name",
                        "value": "P, M",
                        "jive_displayOrder": 31,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "ReportsTo",
                        "value": "529847279",
                        "jive_displayOrder": 37,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Transit Number",
                        "value": "46152",
                        "jive_displayOrder": 38,
                        "jive_showSummaryLabel": false
                    },
                    {
                        "jive_label": "Transit Description",
                        "value": "A B C D",
                        "jive_displayOrder": 39,
                        "jive_showSummaryLabel": false
                    }
                ],
        }

预期输出:enter image description here

2 个答案:

答案 0 :(得分:0)

使用以下代码:

import json
import pandas as pd

with open('data.json') as fd:
    data = json.load(fd)

profile = data['jive']['profile']
headers = [e['jive_label'] for e in profile]
values  = [e['value']      for e in profile]

df = pd.DataFrame(columns=headers, data=[values])

答案 1 :(得分:0)

使用pandas from_dict是解决此问题的一种方法:

# normalize your dict and drop unwanted fields
data = pd.DataFrame.from_dict(myjson['jive']['profile']).drop(['jive_displayOrder','jive_showSummaryLabel'], axis=1)

# reshape the data
data = pd.pivot_table(data, index=None, columns='jive_label', values='value', aggfunc='max')

# remove unwanted names
data.columns.name = None
data.reset_index(drop=True)

print(data)

   BUFUGU  BusinessPurpose COMPANY ID   City    Company     Country     Department  Display Name    Postal/Zip Code Preferred Language  Province/State  ReportsTo Street Address Title Transit Description  Transit Number  XYZ Company Code
0   C06     C0820   333333  NYC     XYZ     United States   46152   P, M    00000   E   NY  529847279   30 NY NY    Analyst     A B C D     46152   DA01
相关问题