我有一个json文件(如下所示),我需要提取一个数据帧,其中列名作为“jive_label”在profile[]内,值作为其值,预期输出如下所示。另外,我给出了示例代码,其中我试图只打印一列,但该代码显然行不通。在
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
}
],
}
使用pandas
from_dict
是解决这一问题的一种方法:使用以下代码:
相关问题 更多 >
编程相关推荐