преобразовать JSON файл с несколькими ключами в один pandas DataFrame - PullRequest
0 голосов
/ 06 мая 2020

Я пытаюсь преобразовать этот JSON файл в фрейм данных, я использую json_normalize("key_name") Я могу преобразовать один ключ в один фрейм данных, но мне нужно преобразовать все ключи в один фрейм данных

{
    "dlg-00a7a82b": [
        {
            "utteranceID": 0,
            "source": "Hi there! How can I help?",
            "target": "Hallo! Wie kann ich helfen?"
        },
        {
            "utteranceID": 1,
            "source": "Hey, ich muss mein Auto zum Mechaniker bringen und ich würde gerne Intelligent Auto Imports besuchen.",
            "target": "Hey there, I need to take my car to mechanic and I would like to see Intelligent Auto imports."
        }
    
     ],
    "dlg-00abd8c8": [
        {
            "utteranceID": 0,
            "source": "hey there, how can i help you today?",
            "target": "Hallo, wie kann ich Ihnen heute helfen?"
        },
        {
            "utteranceID": 1,
            "source": "Hallo. Können Sie mir bei der Bestellung einer neuen Pizzeria namens Bella Luna behilflich sein?",
            "target": "Hi. Are you able to assist me by placing an order at a new pizzeria called Bella Luna?"
        },
        {
            "utteranceID": 2,
            "speaker": "agent",
            "source": "i sure can. what would u like to order from bella luna?",
            "target": "Sicher kann ich das. Was möchten Sie bei bella luna bestellen?"
        }]

}

как этот фрейм данных

https://i.stack.imgur.com/lA3Dy.png

Ответы [ 2 ]

0 голосов
/ 06 мая 2020

Вы можете использовать, например, itertools.chain для загрузки данных (json_data это ваши данные из вопроса):

import pandas as pd
from itertools import chain

df = pd.DataFrame(chain.from_iterable(json_data.values()))

print(df)

EDIT:

Попробуйте поставить list() по цепочке, чтобы поместить данные в список:

df = pd.DataFrame(list(chain.from_iterable(json_data.values())))

Печать:

   utteranceID                                             source                                             target speaker
0            0                          Hi there! How can I help?                        Hallo! Wie kann ich helfen?     NaN
1            1  Hey, ich muss mein Auto zum Mechaniker bringen...  Hey there, I need to take my car to mechanic a...     NaN
2            0               hey there, how can i help you today?            Hallo, wie kann ich Ihnen heute helfen?     NaN
3            1  Hallo. Können Sie mir bei der Bestellung einer...  Hi. Are you able to assist me by placing an or...     NaN
4            2  i sure can. what would u like to order from be...  Sicher kann ich das. Was möchten Sie bei bella...   agent
0 голосов
/ 06 мая 2020

Это может помочь вам

ваш json как sample_ json

list_json = sample_json['dlg-00a7a82b']
parsed_json = json.dumps(list_json)
dict_json = json.loads(parsed_json)
df = pandas.DataFrame(dict_json, columns=dict_json[0].keys())

вывод:

utteranceID                                             source                                             target
0            0                          Hi there! How can I help?                        Hallo! Wie kann ich helfen?
1            1  Hey, ich muss mein Auto zum Mechaniker bringen...  Hey there, I need to take my car to mechanic a...
2            0               hey there, how can i help you today?            Hallo, wie kann ich Ihnen heute helfen?
3            1  Hallo. Können Sie mir bei der Bestellung einer...  Hi. Are you able to assist me by placing an or...
4            2  i sure can. what would u like to order from be...  Sicher kann ich das. Was möchten Sie bei bella...
...