Python pandas将CSV转换为“n”级嵌套JSON?

时间:2017-04-04 09:13:50

标签: json python-2.7 pandas

我想将csv文件转换为嵌套的json到n级。我使用下面的代码从this链接获取所需的输出。但是我收到了一个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-109-89d84e9a61bf> in <module>()
     40     # make a list of keys
     41     keys_list = []
---> 42     for item in d['children']:
     43         keys_list.append(item['name'])
     44 

TypeError: 'datetime.date' object has no attribute '__getitem__'  

以下是代码:

 # CSV 2 flare.json
    # convert a csv file to flare.json for use with many D3.js viz's
    # This script creates outputs a flare.json file with 2 levels of nesting.
    # For additional nested layers, add them in lines 32 - 47
    # sample: http://bl.ocks.org/mbostock/1283663

    # author: Andrew Heekin
    # MIT License

    import pandas as pd
    import json


    df = pd.read_csv('file.csv')


    # choose columns to keep, in the desired nested json hierarchical order
    df = df[['Group','year','quarter']]

    # order in the groupby here matters, it determines the json nesting
    # the groupby call makes a pandas series by grouping 'the_parent' and 'the_child', while summing the numerical column 'child_size'
    df1 = df.groupby(['Group','year','quarter'])['quarter'].count()
    df1 = df1.reset_index(name = "count")

    #print df1.head()
    # start a new flare.json document
    flare = dict()
    flare = {"name":"flare", "children": []}


    #df1['year'] = [str(yr) for yr in df1['year']]



    for line in df1.values:
        the_parent = line[0]
        the_child = line[1]
        child_size = line[2]

        # make a list of keys
        keys_list = []
        for item in d['children']:
            keys_list.append(item['name'])

        # if 'the_parent' is NOT a key in the flare.json yet, append it
        if not the_parent in keys_list:
            d['children'].append({"name":the_parent, "children":[{"name":the_child, "size":child_size}]})

        # if 'the_parent' IS a key in the flare.json, add a new child to it
        else:
            d['children'][keys_list.index(the_parent)]['children'].append({"name":the_child, "size":child_size})

    flare = d


    # export the final result to a json file
    with open('flare.json', 'w') as outfile:
        json.dump(flare, outfile)

以下格式的预期输出:

{
"name": "stock", 
"children": [
    {"name": "fruits",
    "children": [
        {"name": "berries", 
        "children": [
            {"count": 20, "name": "blueberry"}, 
            {"count": 70, "name": "cranberry"}, 
            {"count": 96, "name": "raspberry"}, 
            {"count": 140, "name": "strawberry"}]
        },
        {"name": "citrus", 
        "children": [
            {"count": 20, "name": "grapefruit"},
            {"count": 120, "name": "lemon"},
            {"count": 50, "name": "orange"}]
        }, 
        {"name": "dried fruit",
        "children": [
            {"count": 25, "name": "dates"}, 
            {"count": 10, "name": "raisins"}]
        }]
    },
    {"name": "vegtables",
    "children": [
        {"name": "green leaf",
        "children": [
            {"count": 19, "name": "cress"},
            {"count": 18, "name": "spinach"}]
        },
        {
        "name": "legumes",
        "children": [
            {"count": 27, "name": "beans"},
            {"count": 12, "name": "chickpea"}]
        }]
    }]
}

请有人帮忙解决此错误。

由于

0 个答案:

没有答案