Как я могу абстрагировать два значения из примера вложенных данных в pandas Dataframe? - PullRequest
4 голосов
/ 07 октября 2019

Я использую Набор данных от Standford (см. Dev Set 2.0). Этот файл в формате JSON. Когда я читаю файл, это словарь, но я изменил его на DF:

import json
json_file = open("dev-v2.0.json", "r")
json_data = json.load(json_file)
json_file.close()

df = pd.DataFrame.from_dict(json_data)
df = df[0:2] # for this example, only a subset

Вся необходимая информация находится в столбце df ['data'] ,В каждой строке содержится так много данных в следующем формате:

{'title': 'Normans', 'paragraphs': [{'qas': [{'question': 'In what country is Normandy located?', 'id': '56ddde6b9a695914005b9628', 'answers': [{'text': 'France', 'answer_start': 159}, {'text': 'France', 'answer_start': 159}, {'text': 'France', 'answer_start': 159}, {'text': 'France', 'answer_start': 159}], 'is_impossible': False}, {'question': 'When were the Normans in Normandy?', 'id': '56ddde6b9a695914005b9629', 'answers': [{'text': '10th and 11th centuries', 'answer_start': 94}, {'text': 'in the 10th and 11th centuries', 'answer_start': 87}

Я хочу запросить все Вопросы и Ответы из всех строк в DF. Итак, в идеале вывод будет таким:

Question                                         Answer 
'In what country is Normandy located?'          'France'
'When were the Normans in Normandy?'            'in the 10th and 11th centuries'

Извините заранее! Я прочитал 'Хороший пример' пост. Но мне было трудно создать воспроизводимые данные для этого примера, так как похоже, что это словарь, со списком внутри, внутри списка небольшой словарь, внутри этого другого словаря, затем снова словарь ... когда я использую print (df ["data"]) , это только печать небольшого подмножества ... (что не помогает воспроизвести эту проблему).

print(df['data'])
0    {'title': 'Normans', 'paragraphs': [{'qas': [{...
1    {'title': 'Computational_complexity_theory', '...
Name: data, dtype: object

Большое спасибо заранее!

Ответы [ 2 ]

1 голос
/ 07 октября 2019

Следующая страница (SQuAD (Stanford Q & A) json в Pandas DataFrame) имеет дело с преобразованием dev-v1.1.json в DataFrame.

1 голос
/ 07 октября 2019

Это должно помочь вам начать.

Не был уверен, как обращаться с ситуациями, когда поле ответа пусто, поэтому вы можете найти лучшее решение. Пример:

"question": " After 1945, what challenged the British empire?", "id": "5ad032b377cf76001a686e0d", "answers": [], "is_impossible": true

import json
import pandas as pd 


with open("dev-v2.0.json", "r") as f:
    data = json.loads(f.read())

questions, answers = [], []

for i in range(len(data["data"])):
    for j in range(len(data["data"][i]["paragraphs"])):
        for k in range(len(data["data"][i]["paragraphs"][j]["qas"])):
            q = data["data"][i]["paragraphs"][j]["qas"][k]["question"]
            try: # only takes first element since the rest of values are duplicated?
                a = data["data"][i]["paragraphs"][j]["qas"][k]["answers"][0]["text"]
            except IndexError: # when `"answers": []`
                a = "None"

            questions.append(q)
            answers.append(a)

d = {
    "Questions": questions,
    "Answers": answers
}

pd.DataFrame(d)

                                               Questions                      Answers
0                   In what country is Normandy located?                       France
1                     When were the Normans in Normandy?      10th and 11th centuries
2          From which countries did the Norse originate?  Denmark, Iceland and Norway
3                              Who was the Norse leader?                        Rollo
4      What century did the Normans first gain their ...                 10th century
...                                                  ...                          ...
11868  What is the seldom used force unit equal to on...                       sthène
11869           What does not have a metric counterpart?                         None
11870  What is the force exerted by standard gravity ...                         None
11871  What force leads to a commonly used unit of mass?                         None
11872        What force is part of the modern SI system?                         None

[11873 rows x 2 columns]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...