簡體   English   中英

如何將 json 轉換為 pandas dataframe?

[英]How to convert json into a pandas dataframe?

我正在嘗試將 api 響應從 json 轉換為 Z3A43B4F88325D9405AZC2C2C2中的 dataframe 我遇到的問題是 de 數據嵌套在 json 格式中,我在 dataframe 中沒有得到正確的列。

數據是從 api 收集的,格式如下:

{'tickets': [{'url': 'https...',
   'id': 1,
   'external_id': None,
   'via': {'channel': 'web',
    'source': {'from': {}, 'to': {}, 'rel': None}},
   'created_at': '2020-05-01T04:16:33Z',
   'updated_at': '2020-05-23T03:02:49Z',
   'type': 'incident',
   'subject': 'Subject',
   'raw_subject': 'Raw subject',
   'description': 'Hi, this is the description',
   'priority': 'normal',
   'status': 'closed',
   'recipient': None,
   'requester_id': 409467360874,
   'submitter_id': 409126461453,
   'assignee_id': 409126461453,
   'organization_id': None,
   'group_id': 360009916453,
   'collaborator_ids': [],
   'follower_ids': [],
   'email_cc_ids': [],
   'forum_topic_id': None,
   'problem_id': None,
   'has_incidents': False,
   'is_public': True,
   'due_at': None,
   'tags': ['tag_1',
    'tag_2',
    'tag_3',
    'tag_4'],
   'custom_fields': [{'id': 360042034433, 'value': 'value of the first custom field'},
    {'id': 360041487874, 'value': 'value of the second custom field'},
    {'id': 360041489414, 'value': 'value of the third custom field'},
    {'id': 360040980053, 'value': 'correo_electrónico'},
    {'id': 360040980373, 'value': 'suscribe_newsletter'},
    {'id': 360042046173, 'value': None},
    {'id': 360041028574, 'value': 'product'},
    {'id': 360042103034, 'value': None}],
   'satisfaction_rating': {'score': 'unoffered'},
   'sharing_agreement_ids': [],
   'comment_count': 2,
   'fields': [{'id': 360042034433, 'value': 'value of the first custom field'},
    {'id': 360041487874, 'value': 'value of the second custom field'},
    {'id': 360041489414, 'value': 'value of the third custom field'},
    {'id': 360040980053, 'value': 'correo_electrónico'},
    {'id': 360040980373, 'value': 'suscribe_newsletter'},
    {'id': 360042046173, 'value': None},
    {'id': 360041028574, 'value': 'product'},
    {'id': 360042103034, 'value': None}],
   'followup_ids': [],
   'ticket_form_id': 360003608013,
   'deleted_ticket_form_id': 360003608013,
   'brand_id': 360004571673,
   'satisfaction_probability': None,
   'allow_channelback': False,
   'allow_attachments': True},

我已經嘗試過以下內容:我已將 JSON 格式轉換為字典,如下所示:

x = response.json()
df = pd.DataFrame(x['tickets'])

但我正在為 output 苦苦掙扎。 我不知道如何獲得正確、有序、標准化的 dataframe。

(我是新來的:))

假設您通過此代碼獲取請求數據r = requests.get(url, auth)

你的數據還不清楚,所以讓我們得到一個 dataframe data = pd.read_json(json.dumps(r.json, ensure_ascii = False))

但是,您可能會得到一個單排的 dataframe。

當我遇到這樣的問題時,我寫了這個 function 來獲取完整的數據:

listParam = []

def listDict(entry):
    if type(entry) is dict:
        listParam.append(entry)
    elif type(entry) is list:
        for ent in entry:
            listDict(ent)

因為 {'tickets': ...} 你的數據看起來像一個字典,你需要得到這樣的信息:

listDict(data.iloc[0][0])

接着,

pd.DataFrame(listParam)

我無法顯示結果,因為您沒有發布完整的數據,也沒有告訴我在哪里可以找到要測試的數據,但這可能會奏效。

您必須先將 json 轉換為字典,然后將鍵 'tickets' 的字典值轉換為 dataframe。

file = open('file.json').read()
ticketDictionary = json.loads(file)
df = pd.DataFrame(ticketDictionary['tickets'])

'file.json'在此處包含您的數據。

df現在包含這種格式的 dataFrame。 df

對於響應中的列表,如果需要,您可以使用單獨的數據框:

for field in df['fields']:
        df = pd.DataFrame(field)

它會給你這個長度:

            id                             value
0  360042034433   value of the first custom field
1  360041487874  value of the second custom field
2  360041489414   value of the third custom field
3  360040980053                correo_electrónico
4  360040980373               suscribe_newsletter
5  360042046173                              None
6  360041028574                           product
7  360042103034                              None

這可能是一種結構方式,因為您沒有提到確切的預期格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM