Поиск…
Читать JSON
может либо передать строку json, либо путь к файлу с действительным json
In [99]: pd.read_json('[{"A": 1, "B": 2}, {"A": 3, "B": 4}]')
Out[99]:
   A  B
0  1  2
1  3  4
В качестве альтернативы для сохранения памяти:
with open('test.json') as f:
    data = pd.DataFrame(json.loads(line) for line in f)
Dataframe во вложенный JSON, как в файлах flare.js, используемых в D3.js
def to_flare_json(df, filename):
    """Convert dataframe into nested JSON as in flare files used for D3.js"""
    flare = dict()
    d = {"name":"flare", "children": []}
    
    for index, row in df.iterrows():
        parent = row[0]
        child = row[1]
        child_size = row[2]
        # Make a list of keys
        key_list = []
        for item in d['children']:
            key_list.append(item['name'])
        #if 'parent' is NOT a key in flare.JSON, append it
        if not parent in key_list:
            d['children'].append({"name": parent, "children":[{"value": child_size, "name": child}]})
        # if parent IS a key in flare.json, add a new child to it
        else:
            d['children'][key_list.index(parent)]['children'].append({"value": child_size, "name": child})
    flare = d
    # export the final result to a json file
    with open(filename +'.json', 'w') as outfile:
        json.dump(flare, outfile, indent=4)
    return ("Done")
Чтение JSON из файла
Содержимое файла file.json (один объект JSON в строке):
{"A": 1, "B": 2}
{"A": 3, "B": 4}
Как читать непосредственно из локального файла:
pd.read_json('file.json', lines=True)
# Output:
#    A  B
# 0  1  2
# 1  3  4
Modified text is an extract of the original Stack Overflow Documentation
        Лицензировано согласно CC BY-SA 3.0
        Не связан с Stack Overflow